jq table后台数据动态相加列

此方法稍微有点粗糙,仅供参考

首先定义要计算的列,xmmxList为数据返回的集合

$.each(xmmxList,function(k,s){
var tr = $('<tr class="tr"></tr>');
//表格数据填充
tr.append(
'<td width="20">'+(k+1)+'</td>'+
'<td width="66">'+s.yffylm1d1+'</td><td width="45">'+s.yffylm1d2+'</td>');
$(".table").append(tr);

})

然后是计算核心方法,此方法写在$.each内

var totalRow = 0;
function autoadd(num){
totalRow = 0;
$('.tr').each(function() {
$(this).find('td').eq(num).each(function(){
if($(this).text() === ''){
$(this).text(0);
}
totalRow += parseFloat($(this).text());
});
});
}

动态添加要合计的tr,此方法写在$.each内

$(".table").append('<tr class="tbody_child"><td width="24">合计</td>'+

'<td align="left" width="76" onload="'+autoadd(2)+'">'+totalRow+'</td><td align="left" width="59" onload="'+autoadd(3)+'">'+totalRow+'</td>'</tr>')

 

 

单机版

效果

 

posted @ 2018-03-01 16:19  linsoso  阅读(246)  评论(0)    收藏  举报