【发布时间】:2017-07-24 12:25:01
【问题描述】:
我已经在我的页面上实现了 pipelining example 的 DataTables,我想知道我应该如何访问返回给浏览器的 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)'
);
}
}
} );
} );
现在我对代码有点感兴趣,我看到 DataTables 只是使用页面上的 10 个整体,而不是缓存中返回的完整 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
【问题讨论】:
标签: javascript jquery datatables