【问题标题】:Jquery DataTable column SumJquery DataTable 列总和
【发布时间】:2018-05-20 18:31:07
【问题描述】:

我只是参考this link 来了解如何获取 jquery 数据表中的列总数。但是我已经完成了一半的项目。我在 html 页面中没有任何定义。都包含在 Jquery 端。

在 HTML 中

 <table id="tblCollection" class="display" cellspacing="0" width="100%">
                                        </table>

在 Jquery 中定义如下数据表。

   tblColectionData = $("#tblCollection").DataTable({
        "ordering": true,
        columns: [
            { title: 'OrderId', data: 'OrderId' },
            { title: 'Ordered Date', data: 'OrderPlaceDateTime' },
            { title: 'Customer Name', data: 'CustomerName' },
            { title: 'Restaurant Name', data: 'RestaurantName' },
            { title: 'Order Total', data: 'OrderTotalAmount' }
        ],
    });

如何在我的情况下添加 footerCallback 部分? Web 链接中的示例在 tfoot 中定义。在我的情况下,它不是。如何做到这一点?

编辑 1

将数据填充到数据表中

$.ajax({
    type: 'post',
    url: serverLocation + "/api/dashboard/getOrderData",
    dataType: 'json',
    data: JSON.stringify(reqJson),
    contentType: "application/json; charset=UTF-8",
    success: function (response) {
        tblColectionData.clear().draw();
        tblColectionData.rows.add(response).draw();
    },
    error: function (textStatus, errorThrown) {
        console.log('Err');
    }
});

【问题讨论】:

  • 在示例中是生成总和的 footerCallback 函数。为此,您必须编写自己的代码
  • 这就是我问的怎么办?如何在上述代码中将页脚嵌入到 jquery 数据表部分?
  • "footerCallback": function ( row, data, start, end, display ) { // 你的代码!!! },这就是在你的插件初始化“DataTable({ HERE CALLBACK })”之间

标签: jquery html datatables


【解决方案1】:

获取任何字段总和的最简单方法是:

将'footerCallback'函数添加到DataTable()中,并获取'data'参数。

例子:

$("#example_table").DataTable({
     "footerCallback": function (row, data, start, end, display) {                
                //Get data here 
                console.log(data);
                //Do whatever you want. Example:
                var totalAmount = 0;
                for (var i = 0; i < data.length; i++) {
                    totalAmount += parseFloat(data[i][4]);
                }
                console.log(totalAmount);
       }
})

注意你想要的数据索引的位置。

好好享受吧!!!

【讨论】:

    【解决方案2】:

    假设您想对“订单总列”(第 4 列)求和,您可以按照 Datatables 文档提供的示例进行操作:

    $('#tblCollection').dataTable( {
        ordering: true,
        columns: [
            { title: 'OrderId', data: 'OrderId' },
            { title: 'Ordered Date', data: 'OrderPlaceDateTime' },
            { title: 'Customer Name', data: 'CustomerName' },
            { title: 'Restaurant Name', data: 'RestaurantName' },
            { title: 'Order Total', data: 'OrderTotalAmount' }
        ],
        footerCallback: function( tfoot, data, start, end, display ) {
            var api = this.api();
            $(api.column(4).footer()).html(
                api.column(4).data().reduce(function ( a, b ) {
                    return a + b;
                }, 0)
            );
        }
    });
    

    然后表需要tfoot 来执行回调:

    <table id="tblCollection" class="display" cellspacing="0" width="100%">
        <thead>
             <tr>
                <th>Order id</th>
                <th>Order date</th>
                <th>Customer name</th>
                <th>Restaurant name</th>
                <th>Order total</th>
            </tr>
        </thead>
        <tbody>
             ... your data here ...
        </tbody>
        <tfoot>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
             </tr>
        </tfoot>
    </table>
    

    请注意,如果表格没有 tfoot 元素,则不会触发此回调。

    你可以找到更多关于footerCallbackhere的信息。

    【讨论】:

    • 非常感谢muecas的解释。但我坚持的是,我使用 AJAX 调用将数据填充到数据表中。 (编辑 1 部分)。您可以看到我只是使用它绑定数据值。但是在给定的示例中,该怎么做?
    • @weeraa 为什么不使用 Datatables 实现 ajax 而不是您自己的实现?
    猜你喜欢
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多