【问题标题】:How to do sum of all the textbox in Datatable如何对Datatable中的所有文本框求和
【发布时间】:2021-12-02 04:06:15
【问题描述】:

我正在使用数据表并为一列使用文本框

render: function (data, type, row) {
return '<input class="form-control" id="itemId" name="itemId" maxlength="350" multiline="true" type="text" >';

对于这个特定的列,用户将手动输入数据,我怎样才能对所有这些文本框进行求和。

【问题讨论】:

  • 您的输入字段定义为type="text" - 这意味着可以输入任意文本(数字、文本、数字和文本的混合......)。无论 DataTables 上下文如何,您希望如何求和?为什么不使用type="number"
  • @andrewJames 谢谢,这是关于数字的。对不起,这是我的错。我将其更改为数字。

标签: javascript jquery ajax datatable datatables


【解决方案1】:

您可以使用 DataTables API 访问相关列中的每个 &lt;td&gt; 节点。从那里您可以访问为每个 &lt;input&gt; 字段设置的值,然后对这些值求和。

通过使用 DataTables API 执行此操作,您将确保对所有单元格进行求和,即使是那些未显示在当前页面中的单元格。

这是一个演示:

var dataSet = [
    {
      "id": "1",
      "amount": 1
    },
    {
      "id": "2",
      "amount": 3
    },
    {
      "id": "3",
      "amount": 5
    },
    {
      "id": "4",
      "amount": 2
    },
    {
      "id": "5",
      "amount": 3
    },
    {
      "id": "6",
      "amount": 1
    },
    {
      "id": "7",
      "amount": 4
    },
    {
      "id": "8",
      "amount": 3
    },
    {
      "id": "9",
      "amount": 1
    },
    {
      "id": "10",
      "amount": 2
    },
    {
      "id": "11",
      "amount": 2
    },
    {
      "id": "12",
      "amount": 3
    },
    {
      "id": "13",
      "amount": 1
    },
    {
      "id": "14",
      "amount": 1
    }
  ];
 
$(document).ready(function() {

  var table = $('#example').DataTable( {
    data: dataSet,
    columns: [
      { title: "ID", data: "id" },
      { title: "Amount", data: "amount",
        render: function (data, type, row) {
          return '<input type="number" value="' + data + '">';
        }
      }
    ],
    initComplete: function(settings, json) {
      doSum();
    }
  } );
  
  // This provides the sum of all "approved" records:
  function doSum() {
    // get the DataTables API object:
    var table = $('#example').DataTable();
    // access the <td> cell nodes for the relevant column:
    var nodes = table.column( 1 ).nodes();
    // get the value from each input field in each cell: 
    var total = table.column( 1 ).nodes()
      // and then sum up the integer values:
      .reduce( function ( sum, node ) {
        return sum + parseInt($( node ).find( 'input' ).val());
      }, 0 );
    // place the sum in the relevant footer cell:
    $( table.column( 1 ).footer() ).html( total );
  }
    
  // ensure all changes made by the user are reflected in the 
  // total field:
  $('#example').on( 'change', 'input', function () {
    doSum();
  } );

} );
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Demo</title>
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
  <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">

</head>

<body>

<div style="margin: 20px;">

    <table id="example" class="display dataTable cell-border" style="width:100%">
        <tfoot>
            <th style="text-align: right;">Total:</th><th></th>
        </tfoot>
    </table>

</div>


</body>
</html>

上面的演示假设您在字段中有起始值。当然,如果它们与您的场景无关,您可以删除它们。

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多