jQuery DataTable列的总和 [英] Jquery DataTable column Sum

查看:505
本文介绍了jQuery DataTable列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只引用此链接,以了解如何获取总计列jQuery数据表.但是我已经完成了一半的项目.我在html页面中没有任何定义.全部都包含在Jquery端.

I just refer this link to get idea how to get the column total in jquery data tables. But I have done half of my project. I do not have any definition in html page. all include in Jquery side.

在HTML中

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

定义的数据表,如下面的Jquery.

Defined data table like below in 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 中定义了总数.就我而言,事实并非如此.该怎么做?

How to add footerCallback part in my case? The example in web link is defined the total in tfoot. In my case it is not. How to do this?

编辑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');
    }
});

推荐答案

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

The easiest way to get sum of any field do this:

按如下所示将'footerCallback'函数添加到DataTable()中并获取'data'参数.

Add the 'footerCallback' function into DataTable() as follow and get 'data' parameter.

示例:

$("#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);
       }
})

请注意所需数据索引的位置.

Be careful about the position of data index you want.

享受吧!

这篇关于jQuery DataTable列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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