如何在Datatable中获取textbox列的总和?

时间:2017-12-29 12:55:26

标签: php laravel datatable

我的要求是,在数据表列中使用textbox时,textbox列的总和不在页脚总数中。

          <thead>
            <tr>          
              <th style="width: 30%">Salary Head</th>
              <th style="width: 20%">Amount</th>
              <th style="width: 20%">Type</th>
              <th style="width: 30%">Actual Amount</th>
            </tr>
          </thead>
          <tfoot>
             <tr>
                <th colspan="3" style="text-align:right">Total:</th>
                <th></th>
             </tr>
          </tfoot>
  $('#list_table').DataTable( {
        "searching": false,
        "paging": false,
        "ordering": false,
        "autoWidth": false,
        "bInfo": false,
        "footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;

            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };

            // Total over all pages
            total = api
                .column( 3 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );

             $( api.column( 3 ).footer() ).html(total.toFixed(3));   

        }
    } );
} );

Datatable Normal列正在工作,但是当在Datatable中使用textbox列时,列的总和不起作用。

1 个答案:

答案 0 :(得分:0)

尝试计算总和

var table = $('#example').DataTable();
table.column( 4 ).data().sum(); // as fees is your fourth column

更多信息:

http://www.datatables.net/plug-ins/api/sum()#

https://datatables.net/examples/plug-ins/api.html

否则请参考该链接:

https://codepedia.info/jquery-sum-of-textbox-values-in-table-column/