如何在使用jQuery生成的数据表的最后一行中显示一列的总数? [英] How to display total of a column in the last row of a datatable generated using jQuery?

查看:160
本文介绍了如何在使用jQuery生成的数据表的最后一行中显示一列的总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用jQuery创建了一个数据表.该表有五列,其中一列是付款金额.我必须在付款金额"列底部的付款"列中显示所有值的总和.我该怎么办?

I have created a datatable using jQuery. The table has five columns ,among one column is for payment amount. I have to display the sum of all values in the payment column at the bottom of payment amount column. How can I do that?

下面是我的HTML代码:

Below is my HTML code:

<table id="statement-table" cellpadding="0" cellspacing="0" border="0" class="wide100">
                  <thead>
                        <tr>
                            <th class="black white-active-bg pad-bottom-0-5 cursor-auto">
                            <input type="checkbox" id="select-all-statement" value="" />
                            </th>
                            <th id="stmt-column" class="black white-active-bg pad-bottom-0-5 cursor-auto">Statement</th>
                            <th id="dueDate-column"class="black white-active-bg pad-bottom-0-5 cursor-auto">Due Date</th>
                            <th id="totalDue-column" class="black white-active-bg pad-bottom-0-5 cursor-auto">Total Due</th>
                            <th id="payment-column" class="black white-active-bg pad-bottom-0-5 cursor-auto">Payment Amount</th>
                        </tr>
                        </thead>
               </table>

下面是我的jquery代码:

Below is my jquery code:

 $('#statement-table').dataTable({
         "data": json,
         "dom": 't',
         "info": false,
         "pageLength": 8,
         "columns": [
             {"data":""},
             {"data": "statementCode"},
             {"data": "dueDate"},
             {"data": "statementBalance"},
             {"data": "statementBalance"}

         ],
         "columnDefs": [
             {className: "pad-md-left-p-10 pad-top-bottom-p-10 white-active-bg mouse-link", "targets": [0,1,2,3,4]},
             {
                 'targets':   0,
                 'orderable': false,
                  'render': function(data, type, full, meta) {
                      ++index;
                          return '<input type="checkbox" id="select-checkbox'+index+'" name="payment-checkbox" class="multi-checkbox"/><label for="select-checkbox"></label>';
                  }
             },
             {
                'targets': 1,
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html('<span id="pdf" class="stmt-identifier">'+sData+'</span>');
                }
             },
             {
                'targets': 2,
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).text(date);
                }
             },
             {
                 'targets': 3,
                 'render': function (data, type, full, meta){
                     return '$'+ data;
                 }
              },
              {
                 'targets': 4,
                 'searchable':false,
                 'orderable':false,
                 'render': function (data, type, full, meta){
                     return '<span class="dollar-font">$</span>'+
                     '<input type="number" id="payement-textbox'+index+'" name="payment-textbox" min="0" max="100000" step="any" maxlength="9" class="payment" placeholder="--" value=""/>';
                 }
              }

         ],
         "aaSorting": [[2, 'desc']],

     }); //End of datatable function 

因此,我需要在付款列"的底部打印付款列"的所有值的总和.像这样:

So, I need to print sum of all values of "payment-column" in the bottom of "payment column". Something like this:

请帮助?

推荐答案

请参阅页脚回调有关如何汇总所有页面上数据的示例.

Please see Footer callback example on how to sum data on all pages.

例如,要计算第三列的总数(从零开始的列索引为2):

For example, to calculate total for third column (zero-based column index is 2):

$('#example').DataTable( {
    "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( 2 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Update footer
        $( api.column( 2 ).footer() ).html('$' + total);
    }
} );

这篇关于如何在使用jQuery生成的数据表的最后一行中显示一列的总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆