I'm trying to use https://datatables.net/
Currently we load all the table data in advance and do paging on the client side, which is obviously a bad idea, however I was not able to find a good example of how paging can be done on the server side.
Clearly, I should use ajax option but the documentation is very poor, see https://datatables.net/reference/option/ajax
It says the syntax is as follows:
$('#example').dataTable( {
"ajax": function (data, callback, settings) {
callback(
JSON.parse( localStorage.getItem('dataTablesData') )
);
}
} );
Callback function that must be executed when the required data has been obtained. That data should be passed into the callback as the only parameter
But what is the format of this only parameter??
I tried to pass an array (same array as I would pass data option) but I get:
datatables.bundle.min.js:1 Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
at datatables.bundle.min.js:1
at E (datatables.bundle.min.js:1)
at PatientsSearchTable.dataFetcher (PatientSearchUIController.js:163)
at gA (datatables.bundle.min.js:1)
at pA (datatables.bundle.min.js:1)
at P (datatables.bundle.min.js:1)
at HTMLTableElement.<anonymous> (datatables.bundle.min.js:1)
at Function.each (jquery.min.js:2)
at w.fn.init.each (jquery.min.js:2)
at w.fn.init.o [as dataTable] (datatables.bundle.min.js:1)
e.g. the code works with the following config
let config = {
data: [[1,2],[3,4]],
bDestroy: true,
columns: [
{ title: 'A' },
{ title: 'B' },
],
}
but the following gives the above error:
dataFetcher(data, callback, settings) {
callback([[1,2],[3,4]])
}
generateTableData(dataSet) {
let config = {
ajax: this.dataFetcher.bind(this),
bDestroy: true,
columns: [
{ title: 'A' },
{ title: 'B' },
],
}
EDIT: I was able to get the following to work
dataFetcher(data, callback, settings) {
let jData =[[1,2],[3,4]]
var dtData = {"data": jData}
callback(dtData)
}
generateTableData(dataSet) {
let config = {
ajax: this.dataFetcher.bind(this),
bDestroy: true,
columns: [
{ title: 'A' },
{ title: 'B' },
],
}
thanks to How to populate a JQuery datatable using AJAX option
NOTE: no need to stringify as in var dtData =JSON.stringify( {"data": jData});
But I still need the format of this only parameter, for example, how do I pass the number of result for the paging to show how many pages in total.
EDIT2:
I was able to get paging working as well:
dataFetcher(data, callback, settings) {
let jData = [[1, 2], [3, 4]]
var dtData = {
"data": jData,
"draw": 1,
"recordsTotal": 10,
"recordsFiltered": 10,
}
callback(dtData)
}
generateTableData(dataSet) {
let config = {
ajax: this.dataFetcher.bind(this),
bDestroy: true,
columns: [
{ title: 'A' },
{ title: 'B' },
],
pageLength: 2,
serverSide: true
}
NOTE: you can see this data returned in the following example: https://datatables.net/examples/data_sources/server_side


recordsTotalfield in the returned data. (Side note: There is no specific reason why you need to be using thecallbackvariant of the DataTablesajaxcall. You can (typically) just use the plain version of the call.)