LayUI数据表格操作合计数值

时间:2024-03-12 08:15:39
 1 <script src="~/Scripts/jquery-3.4.1.min.js"></script>
 2 <script src="~/Content/layui/layui.js"></script>
 3 <script>
 4     layui.use(\'table\', function () {
 5         var table = layui.table;
 6 
 7         table.render({
 8               elem: \'#test\'
 9             , url: \'/demo/table/user/\'
10             , cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
11             , totalRow: true //开启合计行
12             , cols: [[
13                   { field: \'id\', width: 80, title: \'ID\', sort: true, totalRowText: \'合计行\' }
14                 , { field: \'username\', width: 80, title: \'用户名\' }
15                 , { field: \'sex\', width: 80, title: \'性别\', sort: true }
16                 , { field: \'city\', width: 80, title: \'城市\' }
17                 , { field: \'sign\', title: \'签名\', width: \'30%\', minWidth: 100 } //minWidth:局部定义当前单元格的最小宽度,layui 2.2.1 新增
18                 , { field: \'experience\', title: \'积分\', sort: true }
19                 , { field: \'score\', title: \'评分\', sort: true,totalRow: true }
20                 , { field: \'classify\', title: \'职业\' }
21                 , { field: \'wealth\', width: 137, title: \'财富\', sort: true }
22             ]]
23 
24             , done: function (res, curr, count) {//数据渲染完的回调。
25 
26                 var rowNums = res.list.length;//获取行数
27 
28                 var scoreTotal = this.elem.next().find(\'.layui-table-total td[data-field="score"] .layui-table-cell\').text();//获取“评分”合计行的合计结果
29                 var scoreAvg = (scoreTotal / rowNums).toFixed(2).toString().concat(\'%\');//计算平均分:总分除以行数,保留两位小数
30                 this.elem.next().find(\'.layui-table-total td[data-field="score"] .layui-table-cell\').text(scoreAvg);//将计算结果复制给“评分”列的合计行
31             }
32 
33             $(\'th\').css({ \'background-color\': \'#ccdeff\', \'color\': \'black\', \'font-weight\': \'bold\' });
34             var that = this.elem.next();
35 
36             //为该数据表格添加样式,间隔行添加颜色
37             res.list.forEach(function (item, index) {
38                 if (index % 2 == 0) {
39                     var tr = that.find(".layui-table-box tbody tr[data-index=\'" + index + "\']").css("background-color", "#FFFFFF");
40                 } else {
41                     var tr = that.find(".layui-table-box tbody tr[data-index=\'" + index + "\']").css("background-color", "#F4F6F9");
42                 }
43             });
44 
45         });
46     });
47 </script>