Get all data when using server side processing
Get all data when using server side processing
We're experimenting with data tables on a new project and due to the large amount of data we are working with, we are using the server side processing of data tables and the scroller plugin. You can sort the columns and filter the records. This is working pretty great, but right now we're trying to figure out how to get all the data from the server through data tables. I would have expected that fnGetData() would have made another query to the server to get all the records (filtered by the users input) but it only appears to return the records that are currently being loaded. Is there a way, given our configuration of dataTables, to get it to retrieve all the records from the server. I would think this wouldnt be very difficult. Just recreate the aoData object sent to the server, but with an iDisplayLength parameter missing or set to -1 or size of all records.
Something like this would be very useful; as well as a fnGetAllData() that fetches all data ignoring filter input.
Something like this would be very useful; as well as a fnGetAllData() that fetches all data ignoring filter input.
This discussion has been closed.
Replies
That is correct. Perhaps I'm missing something, but if you require the ability to get all data, why not just disable server-side processing, and get all the data? With deferred rendering, its normally the get that is the bottle neck.
Allan
Allan
UPDATE: original code I posted had some issues, I've replaced it with the updated.
[code]
/**
* fires the call back passing it all data related to the dataTable. This is different
* from fnGetData in that if the dataTable is bound to server side data, it will call
* to get the representative data from the server before returning. If the dataTable is
* not bound to server side data, it will call the call back passing all the data.
*
* @param {Object} oSettings
* @param {Object} callback
*
* @author Aaron Loes
*/
$.fn.dataTableExt.oApi.fnGetAllRecords = function(oSettings, callback) {
var aoDataReplacements = {
"iDisplayStart": 0,
"iDisplayLength": -1,
"sSearch": ""
};
if (oSettings.oFeatures.bServerSide) {
oSettings.iDraw++;
var aoData = this.oApi._fnAjaxParameters(oSettings);
this.oApi._fnServerParams(oSettings, aoData);
aoData = $.grep(aoData, function(entry) {
if (aoDataReplacements.hasOwnProperty(entry.name)) {
entry.value = aoDataReplacements[entry.name];
}
return entry;
});
oSettings.fnServerData.call(
oSettings.oInstance,
oSettings.sAjaxSource,
aoData,
function(json) {
callback(json.aaData);
}, oSettings );
} else {
callback(this.oApi._fnGetDataMaster(oSettings));
}
};
[/code]
[code]
$("#myDataTable").dataTable().fnGetAllRecords();
[/code]
Allan
UPDATE: fixed bug
[code]
/**
* fires the call back passing it all the filtered data related to the dataTable.If
* the dataTable is bound to server side data, it will call to get the representative
* data from the server before returning. If the dataTable is not bound to server side
* data, it will call the call back passing all the data.
*
* @param {Object} oSettings
* @param {Object} callback
*
* @author Aaron Loes
*/
$.fn.dataTableExt.oApi.fnGetDisplayRecords = function(oSettings, callback) {
var aoDataReplacements = {
"iDisplayStart": 0,
"iDisplayLength": -1
};
if (oSettings.oFeatures.bServerSide) {
oSettings.iDraw++;
var aoData = this.oApi._fnAjaxParameters(oSettings);
this.oApi._fnServerParams(oSettings, aoData);
aoData = $.grep(aoData, function(entry) {
if (aoDataReplacements.hasOwnProperty(entry.name)) {
entry.value = aoDataReplacements[entry.name];
}
return entry;
});
oSettings.fnServerData.call(
oSettings.oInstance,
oSettings.sAjaxSource,
aoData,
function(json) {
callback(json.aaData);
}, oSettings );
} else {
var aData = [];
for (var i = 0 ; i < oSettings.aiDisplay.length; i++) {
aData.push(oSettings.aoData[oSettings.aiDisplay[i]]._aData);
}
callback(aData);
}
};
[/code]
and here's how you call it
[code]
$("#myDataTable").dataTable().fnGetDisplayRecords();
[/code]