table 编辑时更新合计项

分享 未结 3 573
时光与你
时光与你 2019-7-10
悬赏:20飞吻
layui.use(['table'], function () {
var table = layui.table;
partsTable = table.render({
elem: '#parts',
toolbar: '#partsToolbar',
page: true,
limit: 10,
height: 'full-250',
totalRow: true,
cols: cols,
done: function (res, curr, count) {

}
});
table.on('edit(parts)', function (obj) {
obj.update({
peij_sl: response.data.peij_sl + obj.data.peij_dw,
});
tabTotal(partsTable);
});
})
/*
* 更新表格合计
* @param {obj} obj table 对象
*/
var tabTotal = function (obj) {
var layui_table = obj.config.elem.next('.layui-table-view');
var table_data = {};
var table_total_field = [];
layui.use(['table'], function () {
var table = layui.table;
table_data = table.cache[obj.config.id];
var table_cols = obj.config.cols[0];
// 获取合计项
$.each(table_cols, function (i, v) {
if (v.totalRow) {
table_total_field.push(v.field);
}
});
})
var layui_table_total = layui_table.find('.layui-table-total');
$.each(table_total_field, function (i, v) {
// 合计
var sum = table_data.reduce(function (num1, num2) {
// 精度处理
const num1Digits = (num1.toString().split('.')[1] || '').length;
const new_num2 = num2[v] ? num2[v] : 0;
const num2Digits = (new_num2.toString().split('.')[1] || '').length;
const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
return (num1 * baseNum + new_num2 * baseNum) / baseNum;

}, 0);
layui_table_total.find('td[data-field=' + v + '] div').text(sum);
});
}
哈哈 业务需求简单的实现一下下[嘻嘻]
回帖