jQuery通过json的方式响应map的list对象集合,并以table的形式展示
2016-12-16 17:00
555 查看
controller收到ajax请求,处理完成后作出响应,返回map的list对象集合,
@RequestMapping(value = "/getPartByPurchBillId")
@ResponseBody
public Map<String,Object> getPartByPurchBillId(@RequestParam(value="purchBillId") Integer purchBillId){
Map<String, Object> map = new HashMap<>();
List<RequirePartListBean> requirePartListBeans = purchaseService.getPartByPurchBillId(purchBillId);
map.put("requirePartListBeans", requirePartListBeans);
return map;
}
但是因为是在JavaScript中,不能使用<c:foreach />进行遍历,所以使用jQuery方式遍历$.each(),jsp页面如下:
方法被调用时,ajax发送请求,
<script>
function showPartDetails(purchBillId){
var tbody = "";
$.ajax({
type:"post",
url:"${rootPath }/depot/purchase/getPartByPurchBillId?purchBillId="+purchBillId,
success : function(data){
$.each(data.requirePartListBeans, function (n, value) {
//n指索引,requirePartListBeans指valuemap集合中放的list,value指list遍历的对象
//partNumber、partQuantity指对象的属性
var trs = "";
trs += "<tr><td>" + (n+1) + "</td><td>" + value.partNumber + "</td> <td>" + value.partName+ "</td> <td>" + value.partQuantity+ "</td></tr>";
tbody += trs;
});
$("#partList").html(tbody);//这里动态的为tbody添加内容
layer.open({//这里使用layer前端框架,也可以不用
type: 1,
title: false,
closeBtn: 0,
shadeClose: true,
content: $("#showDetail")//打开下面的div,弹窗,遍历的内容进行展示
});
}
});
}
</script>
<%--配件详情部分 --%>
<div id="showDetail" style="display: none;" class="layui-layer layui-layer-dialog layer-anim dialog" type="dialog" times="3" showtime="0" contype="string" >
<div class="layui-layer-title" style="cursor: move;">配件详情</div>
<div id="" class="layui-layer-content">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>配件编号</th>
<th>配件名称</th>
<th>配件数量</th>
</tr>
</thead>
<tbody id="partList">
</tbody>
</table>
</div>
<span class="layui-layer-setwin">
<a class="layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;"></a>
</span>
<div class="layui-layer-btn layui-layer-btn-" >
<a class="layui-layer-btn0" >确定</a>
</div>
<span class="layui-layer-resize"></span>
</div>
<%--end 配件详情部分--%>
@RequestMapping(value = "/getPartByPurchBillId")
@ResponseBody
public Map<String,Object> getPartByPurchBillId(@RequestParam(value="purchBillId") Integer purchBillId){
Map<String, Object> map = new HashMap<>();
List<RequirePartListBean> requirePartListBeans = purchaseService.getPartByPurchBillId(purchBillId);
map.put("requirePartListBeans", requirePartListBeans);
return map;
}
但是因为是在JavaScript中,不能使用<c:foreach />进行遍历,所以使用jQuery方式遍历$.each(),jsp页面如下:
方法被调用时,ajax发送请求,
<script>
function showPartDetails(purchBillId){
var tbody = "";
$.ajax({
type:"post",
url:"${rootPath }/depot/purchase/getPartByPurchBillId?purchBillId="+purchBillId,
success : function(data){
$.each(data.requirePartListBeans, function (n, value) {
//n指索引,requirePartListBeans指valuemap集合中放的list,value指list遍历的对象
//partNumber、partQuantity指对象的属性
var trs = "";
trs += "<tr><td>" + (n+1) + "</td><td>" + value.partNumber + "</td> <td>" + value.partName+ "</td> <td>" + value.partQuantity+ "</td></tr>";
tbody += trs;
});
$("#partList").html(tbody);//这里动态的为tbody添加内容
layer.open({//这里使用layer前端框架,也可以不用
type: 1,
title: false,
closeBtn: 0,
shadeClose: true,
content: $("#showDetail")//打开下面的div,弹窗,遍历的内容进行展示
});
}
});
}
</script>
<%--配件详情部分 --%>
<div id="showDetail" style="display: none;" class="layui-layer layui-layer-dialog layer-anim dialog" type="dialog" times="3" showtime="0" contype="string" >
<div class="layui-layer-title" style="cursor: move;">配件详情</div>
<div id="" class="layui-layer-content">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>配件编号</th>
<th>配件名称</th>
<th>配件数量</th>
</tr>
</thead>
<tbody id="partList">
</tbody>
</table>
</div>
<span class="layui-layer-setwin">
<a class="layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;"></a>
</span>
<div class="layui-layer-btn layui-layer-btn-" >
<a class="layui-layer-btn0" >确定</a>
</div>
<span class="layui-layer-resize"></span>
</div>
<%--end 配件详情部分--%>
相关文章推荐
- 【js jQuery】map集合 循环迭代取值---以及 map、json对象、list、array循环迭代的方法和区别
- 通过Ajax请求后台数据,返回JSONArray(JsonObject),页面(Jquery)以table的形式展示
- SSH JQuery Json ajax请求返回List对象集合
- java方式的将java对象以及list或者map转化为json数据
- 将List集合中的map对象转为List<对象>形式--封装类
- 关于数据序列化(3),JSON的方式,FastJson序列化对象和List集合示例
- java方式的将java对象以及list或者map转化为json数据
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- 将List集合中的map对象转为List<对象>形式实例代码
- java方式的将java对象以及list或者map转化为json数据,json转换为object或者map
- java方式的将java对象以及list或者map转化为json数据 .
- 使用xStream对(Java对象、List集合、Map )到JSON转换、从JSON转换java对象
- 通过FastJson把字符串转换成JSON和Map和List对象处理json数据
- 【第15篇】通过FastJson把字符串转换成JSON和Map和List对象处理json数据
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- 转载:java方式的将java对象以及list或者map转化为json数据 .
- struts2返回JSON,jquery解析JSON(返回的可能是LIST,MAP,对象,单个值)
- Java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- json对象(json-lib)转换成list-map集合