首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >所有页面上的DataTables管道总数

所有页面上的DataTables管道总数
EN

Stack Overflow用户
提问于 2017-07-24 12:25:01
回答 2查看 1.6K关注 0票数 3

我已经在我的页面上实现了流水线实例 of DataTables,我想知道该由谁来访问返回到浏览器的json_encode值

我想要计算返回值的一个特定列的总数,这样我就可以将它显示给用户作为所有页面的总和。

我的jquery代码

代码语言:javascript
运行
复制
table = $('#table').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": $.fn.dataTable.pipeline( {
        url: "<?php echo site_url('test/proj_time_spent')?>/" + projectNum +"/" + VO,
        pages: 6 // number of pages to cache
    } ),

    "footerCallback": function () {
        var api = this.api(), data;

        // Remove the formatting to get integer data for summation
        var intVal = function ( i ) {
            return typeof i === 'string' ?
                i = (Number(i.substr(i.indexOf(">")+1,2))* 3600 + Number(i.substr(i.indexOf(":") + 1,2)) *60 ) / 3600:
                typeof i === 'number' ?
                    i : 0;
        };

        var pageTotal;
        var colTotal;

        for($i = 4; $i <= 4; $i++){
        // Total over this page
        pageTotal = api
            .column( $i, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Total over all pages
        // still returns the same value as the page total above
        colTotal = api
            .column( $i) 
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Update footer of Value
        $( api.column( $i ).footer() ).html(
            pageTotal.toFixed(1) + ' hours <br>(' + colTotal.toFixed(1) + ' hours)' 
        );
        }
    }
} );
} );

现在,我对代码有了一点兴趣,我看到了DataTables只是在使用页面上的10个实体,而不是缓存中返回的全部xhr数据。

编辑我的答案

我将页面数设置为1000个,因此绘制的记录数为1000x10 = 10000,但我的记录将小于10000。

代码语言:javascript
运行
复制
// Pipelining function for DataTables. To be used to the `ajax` option of DataTables
//
$.fn.dataTable.pipeline = function ( opts ) { ...

//rest of code for pipeline example
....
settings.jqXHR = $.ajax( {
            "type":     conf.method,
            "url":      conf.url,
            "data":     request,
            "dataType": "json",
            "cache":    false,
            "success":  function ( json ) {
                cacheLastJson = $.extend(true, {}, json);
                chart_data = cacheLastJson;
                if ( cacheLower != drawStart ) {
                    json.data.splice( 0, drawStart-cacheLower );
                }
                if ( requestLength >= -1 ) {
                    json.data.splice( requestLength, json.data.length );
                }
                colTotal = 0;
                records = (cacheLastJson.recordsTotal !== cacheLastJson.recordsFiltered)? cacheLastJson.recordsFiltered : cacheLastJson.recordsTotal;
                for ( var i = 0; i < records; i++){

                    colTotal += (Number(cacheLastJson.data[i][4].substr(-5,2))*3600 + Number(cacheLastJson.data[i][4].substr(-2,2))*60)/3600;
                }

                drawCallback( json );
            }
        } );

//remainder of code from example
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-24 18:08:13

在服务器端处理模式中,任何时候只有部分数据可用。因此,您将无法使用column().data() API方法计算总价值。

相反,您需要计算服务器上所有页面的总价值,并在Ajax响应中返回它,请参阅下面示例响应中的col1Total

代码语言:javascript
运行
复制
{
    "draw": 1,
    "recordsTotal": 57,
    "recordsFiltered": 57,
    "col1Total": 999,
    "data": [
    ]
}

然后,您将能够使用ajax.json() API方法访问该数据。

代码语言:javascript
运行
复制
var json = table.ajax.json();
console.log(json['col1Total']);

您可以为其他列添加类似的属性,例如col2Totalcol3Total等。

票数 1
EN

Stack Overflow用户

发布于 2018-09-21 03:59:00

要使用ajax响应中的值更新表外的字段,引用Idham对使用相似问题drawCallback的答案提供了一个适当的解决方案

代码语言:javascript
运行
复制
var myTable = $('#myTable').DataTable( {
    "serverSide": true,
    "ajax": { 
        "url" : "/response.php",
        "method" : "POST"
    },
    "drawCallback": function (settings) { 
        // Here the response
        var response = settings.json;
        console.log(response);
    },
});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45280598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档