LayUI數據表格操作合計數值


 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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM