- layui统计当前页的某字段数量,需要两步
var insTb = common.table.init({
elem: '#inventoryTable',
url: prefix + "/list",
id: 'inventoryTable',
toolbar: '#inventoryToolBar',
modalName: "库存数量",
totalRow: true,//第一步
cols: [
[
{type: 'checkbox'},
{
field: 'inventoryId',
title: '主键ID',
hide: true
},
{
field: 'productName',
title: '产品名称',
width: 250,
totalRowText:"合计:",
},
{
field: 'inventoryQty',
align: 'center',
title: '库存数量',
totalRow: true,//第二步
}
]
],
});
2.但往往这不是我们想要的结果,如果要统计所有页某字段的数据,按照官方文档
https://www.layui.com/doc/modules/table.html
{
"code": 0,
"totalRow": {
"score": "666"
,"experience": "999"
},
"data": [{}, {}],
"msg": "",
"count": 1000
}
啥意思? 就是说后台返回给前端的数据要按照这个格式。
具体需要步两步:
1.需要统计的那个字段,需要由后台传到前端,就是说要改变之前的接口,
public TableDataInfo list(Inventory inventory) {
startPage();
List<Inventory> list = aciWmsInventoryService.selectInventoryList(inventory);
//根据条件把统计的数量查出来
Inventory inventory1=aciWmsInventoryService.selectInventoryTotalRow(inventory);
String totalRow=inventory1.getTotalRow();
//这是后端定义的封装,返回端传数据
return getDataTable_(list,totalRow);
}
//之前是没有totalRow参数的,我加了个,对应TableDataInfo的实体类也要加totalRow的get set方法
protected TableDataInfo getDataTable_(List<?> list,String totalRow) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(0);
rspData.setRows(list);
rspData.setTotal(new PageInfo(list).getTotal());
rspData.setTimestamp(DateUtil.current(false));
rspData.setTotalRow(totalRow);
return rspData;
}
现在html页面控制台拿到的数据就是这样的了,
2.调整layui的数据格式
var insTb = common.table.init({
elem: '#inventoryTable',
url: prefix + "/list",
id: 'inventoryTable',
toolbar: '#inventoryToolBar',
modalName: "库存数量",
totalRow: true,//第一步
//第二步,将原始数据解析成 table 组件所规定的数据
parseData: function(res){
console.log(res)
// 不同版本的layui关键字有所不同,
//下面total可能是count,rows可能是data,
//也可能 table.init()和table.render()的
//关键字不一样,结合控制台打印的数据试一试
return {
"code": res.code, //解析接口状态
"msg": res.msg, //解析提示文本
"total": res.total, //解析数据长度
"rows": res.rows, //解析数据列表
//定义合计的字段
"totalRow": {
"inventoryQty": res.totalRow
}
};
},
cols: [
[
{type: 'checkbox'},
{
field: 'inventoryId',
title: '主键ID',
hide: true
},
{
field: 'productName',
title: '产品名称',
width: 250,
totalRowText:"合计:",
},
{
field: 'inventoryQty',
align: 'center',
title: '库存数量',
}
]
],
});
这样就ok了,