datagrid 页面添加合计值

这篇博客介绍了一个JS函数compute,用于计算Datagrid组件中的数据总和及比率。函数遍历数据行,累加各项数值,并处理可能出现的NaN值。在没有合计行的情况下,函数会在Datagrid末尾新增一行显示统计信息,包括各项总计和百分比。此外,compute函数在Datagrid加载成功时自动触发,确保每次数据更新时都能得到最新的合计值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值