试图对数据表进行求和,但它只是对第一页求和

Trying to do a sum on data tables, but its only suming first page

我正在尝试对数据 table 中的列求和。但它只是总结第一页? RequiredoneBookedtwo 这两个变量仅对数据第一页的值求和,而不是整个数据集的值:s

    var sTable = $('.datatable').dataTable({
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "bScrollCollapse": true,
    "aaSorting": [[1, "desc"]],
    "bServerSide": true,
    "bProcessing": true,
    "sAjaxSource": CycleTimeReport,
    "aoColumns": [
        { "sName": "one", "sClass": "ellipsis" },
        { "sName": "two", "sClass": "ellipsis" }
    ],
    "fnServerData": function (sSource, aoData, fnCallback) {
        var data = new Object();
        data.jsonAOData = JSON.stringify(aoData);
        dataToSend = data;

        $.ajax({
            contentType: "application/json; charset=utf-8",
            type: "POST",
            url: sSource,
            data: JSON.stringify(dataToSend),
            success: function (msg) {
                fnCallback(msg);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (document.Invoice.hostname == "localhost") {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.responseText);
                }
            }
        });
    },
    "fnFooterCallback": function (nRow, aaData, iStart, iEnd, aiDisplay) {

            var Requiredone = 0;
            var Bookedtwo = 0;

            for (var i = 0; i < aaData.length; i++) {
                Requiredone+= aaData[i][2] * 1;
                Bookedtwo += aaData[i][3] * 1;
            }
},
    "oLanguage": {
        "sSearch": "_INPUT_"
    },
    "fnInitComplete": function (oSettings, json) {
    },
    "bSortCellsTop": true
});

您正在使用 server-side 处理数据。所以你的 datatable-object 只有当前显示页面的数据。您必须在 server-side 上实现总和并将其添加到 JSON-response 或在额外的 ajax-request.

中执行

另一种选择是禁用 server-side 处理和 pre-populate table 元素,或者用 [=] 填充 datatales-settings 的 aaData-property 23=] 包含所有数据的对象。