2017-07-24 52 views
2

我实现了我的网页上的数据表的pipelining example,我想知道是谁做我访问返回到浏览器数据表管道总数超过

的json_encode值我要计算总的所有页面返回值的特定列,以便我可以将它显示给用户,作为所有页面的总和。

我的jQuery代码

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

现在我感兴趣的代码一点,我看到什么数据表只是在页面上使用的10个entires,不完整的XHR在高速缓存中返回的数据。

编辑我的回答

我设置的页面数为1000,这样绘制的记录数为1000x10 = 10000,但我的记录将小于10000

// 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 

回答

0

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

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

{ 
    "draw": 1, 
    "recordsTotal": 57, 
    "recordsFiltered": 57, 
    "col1Total": 999, 
    "data": [ 
    ] 
} 

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

var json = table.ajax.json(); 
console.log(json['col1Total']); 

您可以为其它列添加类似的特性,例如col2Totalcol3Total

+0

我已经实现了一个管道,所以我可以画出所有的结果,它显示在页面,目前仍是用户给他一个项目的总数。我已经尝试过在单独的Ajax调用之前获取总数,但后来我经常会得到0或NaN返回。 –

+0

@ Juan-EmilSaayman,如果您的记录少于10,000条,则不需要使用服务器端处理模式或流水线方法。那么你的代码会正常工作,因为所有的数据都可用。 –