datagrid 頁面新增合計值

sky_315發表於2018-05-28
function compute() {
var rows = $("#rep").datagrid("getRows")//獲取當前的資料行

//計算總和
var rtotal = 0, hctotal = 0
, wftotal = 0, zgwctotal = 0, mzgtotal = 0, is_heji = 0;
for (var i = 0; i < rows.length; i++) {
rtotal += parseFloat(rows[i]['completeTask']);
hctotal += parseFloat(rows[i]['totalTask']);
wftotal += parseFloat(rows[i]['zongfindProblem']);
zgwctotal += parseFloat(rows[i]['completeRectification']);
mzgtotal += parseFloat(rows[i]['noCompleteRectification']);

//率:對存在NaN的值轉換(根據實際情況選擇)
var a = (rtotal / hctotal * 100).toFixed(2);
var b = (wftotal / hctotal * 100).toFixed(2);
var c = (zgwctotal / wftotal * 100).toFixed(2);
if (a == "NaN") {
a = 0.00;
}
if (b == "NaN") {
b = 0.00;
}
if (c == "NaN") {
c = 0.00;
}
//排序或者在查詢下一頁的資料也會啟動到compute()函式,所以當產生了合計值行的時候就不在重新新增一行合計,只是對資料進行重新的計算
if (rows[i]['areaname'] == "<b>合計:</b>") {
is_heji = 1;
}}
if (is_heji == 0) {
//新增一行顯示統計資訊
$("#rep").datagrid('appendRow', {
areaname: '<b>合計:</b>',
completeTask: rtotal, totalTask: hctotal, findProblemRate: b,
completeRectificationRate: c, zongfindProblem: wftotal, completeRectification: zgwctotal,
noCompleteRectification: mzgtotal, completeTaskRate: a
});
}
}




$("#rep").datagrid({
onLoadSuccess: compute,//頁面重新整理的時候觸發求和的函式
singleSelect: true, //選中一行的設定
AllowPaging: true,
pagination: true,
pageSize: 20,
pageList: [20, 50, 200, 1000],
rownumbers: true, //行號
iconCls: "icon-save", //圖示
collapsible: true, //隱藏按鈕
border: true,
fit: true,
remoteSort: false
}










相關文章