最近搜索

gpt 写的代码。 对表格进行统计合计,合计指定行。

浏览:140
管理员 2024-03-06 10:19



对指定的列进行添加合计,单列。

$(document).ready(function() {
    var totalNum = 0;
    $('#dataList tbody tr').each(function() {
       var text =  $(this).find('td:nth-child(7)').text();  //这个7是从1数,不是从0数。
       if(isNotEmpty(text)){
           totalNum += parseInt($(this).find('td:nth-child(7)').text());
       }
    });
    var totalRow = '<tr><td  style="text-align: right;"  colspan="6">合计</td><td style="text-align: center;">' + totalNum + '</td>    </tr>';
    $('#dataList').append(totalRow);
});

colspan="6" 这个td占6行。





大于第5列 的列都进行合计。 这个方法比较通用。

function cal_total(){
    var dateColumns = [];
    $('#dataList thead th').each(function(index) {
        console.log(index);
        if (index > 5) { // Skip the first 6 columns
            dateColumns.push(index);
        }
    });
    console.log(dateColumns);
    var totals = Array(dateColumns.length).fill(0);
    $('#dataList tbody tr').each(function() {
        $(this).find('td').each(function(index) {
            if (dateColumns.includes(index)) {
                var value = parseInt($(this).text()) || 0;
                totals[index - 6] += value;
            }
        });
    });
    var totalRow = '<tr><td colspan="6">合计</td>';
    totals.forEach(function(total) {
        totalRow += '<td>' + total + '</td>';
    });
    totalRow += '</tr>';

    $('#dataList tbody').append(totalRow);
}


联系站长

站长微信:xiaomao0055

站长QQ:14496453