我知道我可以使用函数fnGetData()但只能获取我所在页面的数据。我想获取所有数据,甚至是当前未显示的页面。我的目的是获得所有的纬度和经度字段,以便我可以使用Google Maps v3在地图上显示。
我有这段代码但是,它只获取当前页面的数据。
var table = $('#example1 table').dataTable();
var data = table.fnGetData();
console.log(data);
答案 0 :(得分:1)
启用服务器端处理('serverSide': true
)时,1.9.x函数fnGetData()和1.10.x函数data()确实只返回当前页面的数据。
可以通过查看Server-side processing example并在控制台$('#example').dataTable().fnGetData()
或$('#example').DataTable().data()
中运行来确认此行为。
为了检索所有数据,您需要制作一个模仿DataTables行为的单独AJAX请求。
由于您的问题已标记为datatables-1.10
,我假设您使用的是DataTables 1.10.x.您可以使用ajax.params(),它将DataTables提交的数据提供给上一个Ajax请求中的服务器。
function processAllRecords(){
var req = $('#example1 table').DataTable().ajax.params();
// Reset request parameters to retrieve all records
req.start = 0;
req.length = -1;
req.search.value = "";
// Request data
$.ajax({
'url': 'script.php',
'data': req,
'dataType': 'json'
})
.done(function(json){
// json.data is array of data source objects (array/object),
// one for each row
console.log(json);
});
}
答案 1 :(得分:-1)
use the fnSettings().fnRecordsTotal() of the datatable.
example : http://www.datatables.net/forums/discussion/2401/record-count-with-server-side-processing
我之前使用过它,我想我可以给你一个片段:
var table = $('#table ').DataTable(
{
"dom" : 'rtp', // Initiate drag column
"fnDraw" : false,
"serverSide": true,
"ajax" : ... ...
}
}),
"fnDrawCallback" : drawCallBack,
});
unction drawCallBack() {
console.log(this.fnSettings().fnRecordsTotal());
}