JSON数据填充表格——(三)

1.定义页面请求JSON的按钮与定义一个带表头的表格

 请求数据的按钮

<button class="btn btn-primary search_bar_button floatR" id="condition_sub">查询<span class=" glyphicon glyphicon-search span_icon"></span></button>

 带表头的表格

<table class="table table-bordered  table-hover" id="unitTable">
<thead>
<tr>
<th>序号</th>
<th>部门编号</th>
<th>部门名称</th>
<th>上级部门编号</th>
<th>描述</th>
</tr>
</thead>
</table>

2.JS处理按钮点击事件与对传回来的JSON数据进行处理

1.点击请求JSON数据

/**
* 点击查询按钮,按条件查询部门信息
*/
$("#condition_sub").click(function(){
defaultSearch();
});
function defaultSearch () {
$.ajax({
type : "post",
dataType : "json",
url : "searchUnitByCondition.action",
data : {
unitId : $("#unit_id").val(),
unitName : $("#unit_name").val()
},
success : showTable
});
}

2. 服务器端对收到的请求处理

public String searchUnitByCondition(){
List<TBaseUnitInfo> baseUnitInfo;
try {
Map<String,Object> condition = new HashMap<String,Object>();
condition.put("unitId", unitId);
condition.put("unitName", unitName);
System.out.println("map"+condition);
baseUnitInfo = unitService.getUnitByConditon(condition);
//将java对象转化为json对象
JSONArray jsonArray = JSONArray.fromObject(baseUnitInfo);
//返回给Ajax
this.result = jsonArray.toString();
} catch (SQLException e) {
}
return SUCCESS;
}

返回的JSON对象

[{"description":"","unitId":"10","unitName":"科技评估中心","upUnitId":""}]

3.JS对收到的数据填充表格

/**
* 显示表格
*/
function showTable(result) {
var unitList = eval("(" + result + ")");
// 清空表格
$("#unitTable tr:not(:first)").html("");
// 在表格中添加数据
for (var i = 0; i < unitList.length; i++) {
var index = i + 1;
$("#unitTable").append(
"<tr><td>" + index+"</td><td>"
+ unitList[i].unitId + "</td><td>"
+ unitList[i].unitName+ "</td><td>"
+ unitList[i].upUnitId+ "</td><td>"
+ unitList[i].description+ "</td></tr>"
);
}
}

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA4oAAABeCAIAAADqqH0LAAAK3klEQVR4nO3cMZLcNhqGYd+Jt9FRNtBVV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A3xC+IOppGIaJDKPpcVhPl31xtzjm22TqJeaad0/nTeU985SeGE1jDA7hC6KehmGYyEAPdACNMRSEL4h6GoZhIgM90AE0xlAQviDqaRiGiQz0QAfQGENB+IKop2EYJjLQAx1AYwwF4QuinoZhmMhAD3QAjTEUhC+IehqGYSIDPdABNMZQEL4g6mkYhokM9EAH0BhDQfiCqKdhGCYy0AMdQGMMBeELop6GYZjIQA90AI0xFIQviHoahmEiAz3QATTGUBC+IOppGIaJDPRAB9AYQ0H4glyupwAAAEBVLtRTAAAAIAr1FAAAAA2hngIAAKAh1FMAAAA0hHoKAACAhlBPAQAA0BDqKQAAABpCPQUAAEBDqKcAAABoCPUUAAAADaGeAgAAoCHUUwAAADSEegoAAICG+D9LOy6uieF66AAAAABJRU5ErkJggg==" alt="" />

上一篇:C++ STL 初探


下一篇:Java知多少(37)静态内部类、匿名内部类、成员式内部类和局部内部类