layui数据表格开启编辑后,修改表格的值往往合计不能自动更新,这里通过循环数据表格进行更新合计的值
[JavaScript] 纯文本查看 复制代码 //监听单元格编辑
table.on('edit(test)', function(obj){
let data = obj.data;//得到所在行所有键值
let field = obj.field; //得到字段
//计算合计公时与总工资
let table_data = table.cache["test"];//得到数据表格
let totalWorkingHours = 0;//合计总工时
let totalwages = 0;//合计工资
for (let i = 0; i < table_data.length; i++) {
if(isNumber(table_data[i]['workingHours'])){
totalWorkingHours += parseFloat(table_data[i]['workingHours']);
}
if(table_data[i]['wages'] != '0.00'){
totalwages += parseFloat(table_data[i]['wages']);
}
}
$('.layui-table-total td[data-field="wages"] .layui-table-cell').text(totalwages);
$('.layui-table-total td[data-field="workingHours"] .layui-table-cell').text(totalWorkingHours);
}
}); |