首页 文章

如何组合多于2个重复行并使用jquery对html表中的值求和

提问于
浏览
0

我跟 html table 结了 . 我想要结合2 duplicate rows 并将重复行中的值相加 .

我的表格示例:

Name Amount
约翰200
约翰300
约翰100
收获400
收获400

预期结果:

Name Amount
约翰600
哈里什800

我尝试使用下面的jquery代码,但它只有两个重复的行,如果超过两个重复的行发生它不合并但它正在添加值并显示重复的行 .

Html Code:

<table>
    <tr>
        <td>Name</td>
        <td>quantity</td>
        <td>expired</td>
    </tr>
    <tr class="row">
        <td class="id">A</td>
        <td class="val">25</td>
        <td class="date">date</td>
    </tr>
    <tr class="row">
        <td class="id">A</td>
        <td class="val">25</td>
        <td class="date">date</td>
    </tr>
     <tr class="row">
        <td class="id">A</td>
        <td class="val">25</td>
        <td class="date">date</td>
    </tr>
     <tr class="row">
        <td class="id">A</td>
        <td class="val">25</td>
        <td class="date">date</td>
    </tr>
    <tr class="row">
        <td class="id">B</td>
        <td class="val">100</td>
        <td class="date">date</td>
    </tr>
    <tr class="row">
        <td class="id">C</td>
        <td class="val">35</td>
        <td class="date">date</td>
    </tr>
    <tr class="row">
        <td class="id">C</td>
        <td class="val">35</td>
        <td class="date">date</td>
    </tr>
</table>

Jquery Code:

var first_row = '<tr class="row"><td class="id">NULL</td></tr>';
var rowCount = 0;
var rowSum = 0;
$.each($('.row'), function (index, curRow) {
    if ($(first_row).find('.id').text() != $(curRow).find('.id').text()) {

       if (rowCount > 1) {
            $(first_row).find('.val').text(rowSum);
            $(first_row).find('.val').css('background-color','silver');
            for (i = 0; i < rowCount; i++) {
                $(first_row).next('.row').find('.val').remove();
                $(first_row).next('.row').find('.date').remove();
                $(first_row).next('.row').find('.id').remove();

            }
            }
       first_row = $(curRow);
        rowSum = 0;
        rowCount = 0;
    }

    rowSum += parseInt($(curRow).find('.val').text());
    rowCount += 1;
});

if (rowCount > 1) {
    $(first_row).find('.val').text(rowSum);
    $(first_row).find('.val').css('background-color','silver');
    for (i = 0; i < rowCount; i++) {
        $(first_row).next('.row').find('.val').remove();
        $(first_row).next('.row').find('.date').remove();
        $(first_row).next('.row').find('.id').remove();
    }
}

Result:

Name quantity expired

100日期
25日期
25日期
B 100日期
C 70日期

我想在名称列中所有记录应该是 distinct 和要汇总的数量 .

请在这件事上给予我帮助

2 回答

  • 1

    使用jQuery .nextAll().filter()的另一种方法

    $('.row').each(function() {
      var thisId = $(this).find('.id').text();
      var sumVal = parseFloat($(this).find('.val').text());
    
      var $rowsToGroup = $(this).nextAll('tr').filter(function() {
        return $(this).find('.id').text() === thisId;
      });
    
      $rowsToGroup.each(function() {
        sumVal += parseFloat($(this).find('.val').text());
        $(this).remove();
      });
    
      $(this).find('.val').text(sumVal);
    });
    
  • 2

    试试这个:

    var json = {};
        //Sum up everything and erase
        $.each($('.row'), function (index, curRow) {                                    
            var curName=$(curRow).find('.id').text();   
            var curQty=$(curRow).find('.val').text();
            if (json[curName] != null){
                json[curName] += parseInt(curQty);
            } else {
                json[curName] = parseInt(curQty);               
            }
            $(this).remove();
        });
        //Rebuild table
        jQuery.each(json, function(name, val) {
            $('table').append('<tr class="row"><td class="id">'+name+'</td><td class="val">'+val+'</td><td class="date">date</td></tr>');
        });
    

相关问题