如何计算所有行表的一列中的值总和表查找 [英] How to calculate sum of value in one column of all rows table table finding

查看:184
本文介绍了如何计算所有行表的一列中的值总和表查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张这样的桌子:

HTML:

<table>
    <tr><td>value1</td><td>value2</td><td>value3</td></tr>
    ....
    <tr><td>value1</td><td>value2</td><td>value3</td></tr>
</table>

,并希望通过使用jquery添加动态行来计算表底部各行的总和.

and wants to calculate sum of each rows on the bottom of table by adding dynamic rows with jquery.

这是在使用jQuery计算之前的示例表.

this is sample table before calculate with jQuery.

2   3   4
7   1   2
32  58  4

这是结果:

2   3   4
7   1   2
32  58  4
41  62  10

JQuery:

$("tr").each(function(){
    var tdCount= $(this).find("td").length;
    for(i=0;i<tdCount;i++)
    {
        newRow[i]+=parseInt($(this).eq(i).value);
    }
});
var row="<tr>";
for (i=0;i<newRow.length;i++)
    row+="<td>"+newRow[i]+"</td>";
row+="</tr>";
$("table").append(row);

推荐答案

如果需要动态创建带有结果的新行,则可以使用append函数来实现:

If you need create a new row dynamically with results, you can do it with append function:

$(document).ready(function(){
  var result = [];
  $('table tr').each(function(){
    $('td', this).each(function(index, val){
        if(!result[index]) result[index] = 0;
      result[index] += parseInt($(val).text());
    });
  });

  $('table').append('<tr></tr>');
  $(result).each(function(){
    $('table tr').last().append('<td>'+this+'</td>')
  });
});

在此处查看示例:字段

这篇关于如何计算所有行表的一列中的值总和表查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆