循环 HTML 表并将相同的项目及其计算的金额添加到另一个表中

Loop HTML table and add the identical items and its calculated amount to another table

本文关键字:金额 计算 添加 另一个 项目 HTML 循环      更新时间:2023-09-26

<html>
<body>
  <div>
    <table border="1" id="topTable">
      <thead>
        <th>Item</th>
        <th>Sold</th>
      </thead>
      <tbody id="topTableBody">
        <tr>
          <td>Apples</td>
          <td>50</td>
        </tr>
        <tr>
          <td>Apples</td>
          <td>25</td>
        </tr>
        <tr>
          <td>Oranges</td>
          <td>30</td>
        </tr>
        <tr>
          <td>Strawberry</td>
          <td>60</td>
        </tr>
        <tr>
          <td>Cherry</td>
          <td>10</td>
        </tr>
        <tr>
          <td>Guava</td>
          <td>5</td>
        </tr>
        <tr>
          <td>Strawberry</td>
          <td>20</td>
        </tr>
      </tbody>
    </table>
  </div>
  <button id="btn">Click</button>
  </br>
  <div>
    <table border="1" id="bottomTable">
      <thead>
        <th>Item</th>
        <th>Sold</th>
      </thead>
      <tbody id="bottomTableBody">
      </tbody>
    </table>
  </div>
</body>
</html>

当我按下按钮时,我希望它遍历顶部表格并获取相似的项目名称,并将它们与底部表格中的销售金额合并到一行中,例如:苹果将有自己的行,销售金额为 75,而其他没有相似名称的人将有自己的行,例如橙子,其销售金额也是如此。

如果你可以使用 JQuery。(JSFiddle: http://jsfiddle.net/inanda/o9axgkaz/):

  jQuery('#btn').on('click', function() {
        var sumMap = {};
//Iterate through table rows
    $("table tbody tr").each(function () {
        if (sumMap[$(this).children('td:nth-child(1)').text()]) {            
            sumMap[$(this).children('td:nth-child(1)').text()] =  sumMap[$(this).children('td:nth-child(1)').text()] +Number($(this).children('td:nth-child(2)').text());
        } else {
            sumMap[$(this).children('td:nth-child(1)').text()] = Number($(this).children('td:nth-child(2)').text());
        }  
    })
//Append result to the other table
    $.each(sumMap, function (i, val) {
      $('#bottomTable tr:last').after('<tr><td>'+i+'</td><td>'+val+'</td>');
    });
    });

纯JavaScript:(JSFiddle: http://jsfiddle.net/inanda/2dmwudfj/):

appendResultToBottomTable= function() {
    var sumMap = calculate();
    appendResultToTable('bottomTableBody', sumMap);
}
function calculate() {
    var table = document.getElementById("topTableBody");
    var map = {};
for (var i = 0, row; row = table.rows[i]; i++) {
    var itemType=(row.cells[0].innerText || row.cells[0].textContent);
    var value=(row.cells[1].innerText || row.cells[1].textContent);
        if (map[itemType]) {
        map[itemType] =  map[itemType] +Number(value);
    } else {
        map[itemType] = Number(value);
    }    
}
    return map;
}
function appendResultToTable(tableId, sumMap){
    var table = document.getElementById(tableId);
    for (var item in sumMap){
  var row   = table.insertRow(table.rows.length);
  var cellItem  = row.insertCell(0);
        var cellValue  = row.insertCell(1);
  cellItem.appendChild(document.createTextNode(item));
        cellValue.appendChild(document.createTextNode(sumMap[item]));
} 
}

如果项目适用于使用外部库,则可以使用如下所示的代码执行此操作:

alasql('SELECT Item,SUM(CONVERT(INT,Sold)) AS Sold '
    INTO HTML("#res",{headers:true}) '
    FROM HTML("#topTable",{headers:true}) '
    GROUP BY Item');

这里:

  1. SELECT Item, SUM(Sold) FROM data GROUP BY Item 是一个正则 SQL 表达式,用于对表中的数据进行分组和求和
  2. 从字符串到 INT 类型的CONVERT(INT,Sold)转换过程
  3. FROM HTML()INTO HTML()特殊函数来从/向 HTML 表读取/写入数据,{headers:true}是使用标头的参数
  4. 我添加了一些次要的CSS代码(用于表格和单元格边框),因为Alasql生成"纯"HTML表格。

请参阅下面的工作片段。

(免责声明:我是 Alasql 库的作者)

function run() {
    alasql('SELECT Item,SUM(CONVERT(INT,Sold)) AS Sold INTO HTML("#res",{headers:true}) FROM HTML("#topTable",{headers:true}) GROUP BY Item');
}
#res table {
   border:1px solid black;
}
#res table td, th{
   border:1px solid black;
}
<script src="http://alasql.org/console/alasql.min.js"> </script>
<div>
    <table border="1" id="topTable">
      <thead>
        <th>Item</th>
        <th>Sold</th>
      </thead>
      <tbody id="topTableBody">
        <tr>
          <td>Apples</td>
          <td>50</td>
        </tr>
        <tr>
          <td>Apples</td>
          <td>25</td>
        </tr>
        <tr>
          <td>Oranges</td>
          <td>30</td>
        </tr>
        <tr>
          <td>Strawberry</td>
          <td>60</td>
        </tr>
        <tr>
          <td>Cherry</td>
          <td>10</td>
        </tr>
        <tr>
          <td>Guava</td>
          <td>5</td>
        </tr>
        <tr>
          <td>Strawberry</td>
          <td>20</td>
        </tr>
      </tbody>
    </table>
  </div>
  <button id="btn" onclick="run()">Click</button>
  </br>
<div id="res"></div>