How to calculate sum of value in one column of all rows table table finding

后端 未结 3 823
你的背包
你的背包 2021-01-16 06:06

I have a table like this :

HTML:

';
}

$('table').append('' + totalRow + '');

value1value2value3&
3条回答
  •  北海茫月
    2021-01-16 06:56

    You can do it like following.

    $('table tfoot td').each(function(index) {
        var total = 0;
        $('tbody tr').each(function() {
            total += +$('td', this).eq(index).text(); //+ will convert string to number
        });
        $(this).text(total);
    })
    
    
    234
    712
    32584

    Update: for dynamic total row.

    var totalRow = '', columnNo = $('table tr:first td').length;
    
    for (var index = 0; index < columnNo; index++) {
        var total = 0;
        $('table tr').each(function () {
            total += +$('td', this).eq(index).text(); //+ will convert string to number
        });
        totalRow += '
' + total + '
234
712
32584

提交回复
热议问题