【问题标题】:How to sum specific column after filtering过滤后如何对特定列求和
【发布时间】:2021-10-29 05:31:46
【问题描述】:

它是一个从 php 获取数据的简单表格

   <table style="margin-right: 200px;" id="myTable" class="table">
      <thead style="margin-right: 200px;">
        <tr style="font-size: 12px;">
          <th style="font-size: 12px;width: 3%;" class="lead">S.no</th>
          <th style="font-size: 12px;" class="lead">Contract No.</th>
          <th style="font-size: 12px;" class="lead">Commodities</th>
          <th style="font-size: 12px;" class="lead">Seller</th>
          <th style="font-size: 12px;" class="lead">Buyer</th>
          <th style="font-size: 12px;" class="lead">FCL</th>
          <th style="font-size: 12px;" class="lead">Rate</th>
          <th style="font-size: 12px;" class="lead">Weight</th>
          <th style="font-size: 12px;" class="lead">Bill Amount</th>
          <th style="font-size: 12px;" class="lead">Commision</th>
          <th style="font-size: 12px;" class="lead">Shipment Period</th>
          <th style="font-size: 12px;" class="lead">ETD</th>
          <th style="font-size: 12px;" class="lead">ETA</th>
          <th style="font-size: 12px;" class="lead">Mode Amount</th>
          <th style="font-size: 12px;display: none;" class="lead">Mode</th>
          <th style="font-size: 12px;" class="lead">Status</th>
          <th style="font-size: 12px;" class="lead">Action</th>
        </tr>
      </thead>
      <tbody style="font-size: 12px;margin-right: 200px;">
        <?php
        $sql = "SELECT * FROM `indent`";
        $result = mysqli_query($conn, $sql);
        $sno = 0;
        while ($row = mysqli_fetch_assoc($result)) {
          $sno = $sno + 1;
          echo "<tr>
        <th scope='row'>" . $sno . "</th>
        <td>" . $row['contractNo'] . "</td>
        <td>" . $row['commodities'] . "</td>
        <td>" . $row['seller'] . "</td>
        <td>" . $row['buyer'] . "</td>
        <td>" . $row['fcl'] . "</td>
        <td>" . $row['rate'] . "</td>
        <td>" . $row['weight'] . "</td>
        <td>" . $row['billAmount'] . "</td>
        <td id='loop' >" . $row['commision'] . "</td>
        <td>" . $row['shipmentPeriod'] . "</td>
        <td>" . $row['etd'] . "</td>
        <td>" . $row['eta'] . "</td>
        <td>" . $row['modeAmount'] . "</td>
        <td style='display:none;'>" . $row['mode'] . "</td>
        <td>" . $row['statusPayment'] . "</td>
        <td > <button style='font-size: 12px;' data-bs-toggle='modal' data-bs-target='#editModal' class='edit btn btn-sm btn-primary' id=" . $row['sno'] . ">Edit</button> <button style='font-size: 12px;' class='delete btn btn-sm btn-primary' id=d" . $row['sno'] . ">Delete</button>  </td>
      </tr>";
        }
        ?>
      </tbody>
      <tfoot>
        <td id="total">0</td>
      </tfoot>
    </table>

我已经找到了如何对列求和的答案,但是在过滤它时它不会添加正在过滤的值,它只是显示所有的佣金列,汇总了该表来自 datatables.net

这是我正在运行以获取总和的函数

 $(function() {

  var TotalValue = 0;

  $("tr #loop").each(function(index, value) {
    currentRow = parseFloat($(this).text());
    TotalValue += currentRow
  });

  document.getElementById('total').innerHTML = TotalValue;

});

它获得了所有的佣金列,但我希望在过滤后它显示总和值我希望你能理解我想要什么

【问题讨论】:

  • ID 必须在 HTML 文档中是唯一的。
  • 我正在从 sql 中获取 td 值,因此使用 id 可以更轻松地告诉我过滤后如何求和\
  • 使用类而不是 ID。
  • 我将使用该类,但您能告诉我如何在过滤后对值求和
  • 你在说什么“过滤”,我没看到。

标签: javascript php html jquery datatables


【解决方案1】:

只需将代码放入函数中

function get_total(){
     var TotalValue = 0;

   $("tr #loop").each(function(index, value) {
     currentRow = parseFloat($(this).text());
    TotalValue += currentRow
  });

  document.getElementById('total').innerHTML = TotalValue;
}

过滤后调用这个函数

【讨论】:

  • 你能告诉我在使用 datatable.net 提供的搜索输入后如何回忆这个
  • OP 使用的是数据表,它不会渲染过滤/分页的数据,所以处理trs 不会给出所有数据;这是 OP 问题。
  • 你用的是数据表还是普通表
  • 我正在使用来自 datatable.net 的数据表
  • 我无法定位由 datatables.net 提供的搜索输入,因此我无法将 onchange 或 onblur 函数放入其中
猜你喜欢
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 2020-11-09
  • 2014-12-06
  • 1970-01-01
相关资源
最近更新 更多