Understanding use of server-side ajax response....
Understanding use of server-side ajax response....
I'm very confused about server-side documentation.
Actually I have a datatables initilized from DOM, created with a foreach loop in PHP.
The result is 6000 rows... The page freeze for some seconds but at the end of loading all work great: pagination (default set to 50 per page), bFilter, ...
Now i'm trying to migrated to server-side loading but i'm very confusing about documentation.
I tried and my code get results, but pagination is broken (all 6000 results are printed in one page with pagination only 1),... also i don't know meaning of some fields like: "draw" in json ajax response.
How can I fix my code to show only 50 rows on first load of the page?
here is my code:
$('#talbe').DataTable( {
"aaSorting": [[ 0, "asc" ]],
responsive: false,
paging: true,
"pageLength": 50,
"lengthMenu": [ [20, 50, 100, -1], [20, 50, 100, "All"] ],
"info": true,
scrollCollapse: false,
scrollY: false,
scrollX: true,
"columns": [
{ "data": "Field1" },
{ "data": "Field2" },
{ "data": "Field3" },
{ "data": "Field4" },
{ "data": "Field5" },
{ "data": "Field6" }
],
"processing": true,
"serverSide": true,
"ajax": "ajax/load.php",
...
});
Here my load.php
<?php
$res = get_results_example();
$data = array();
foreach ( $res as $r) {
$temp = array();
$temp['Field1'] = $r['Field1'];
$temp['Field2'] = $r['Field2'];
$temp['Field3'] = $r['Field3'];
$temp['Field4'] = $r['Field4'];
$temp['Field5'] = $r['Field5'];
$temp['Field6'] = $r['Field6'];
$data[] = $temp;
}
$result = array(
"draw" => $_GET['draw'], //<---------------------------- it's it ok? what it means?
"recordsTotal" => count($res), // in my example is 6000
"recordsFiltered" => count($res), //<--------------------- what value i need to set here?
"data" => $data //<------- should load all 6000 result?
);
echo json_encode($result);
?>
Answers
You probably have seen this but the docs are here:
https://datatables.net/manual/server-side
The
draw
parameter is a sequence that the Datatables client uses to know when it receives the proper response. The response needs to have the same number as the request.The
recordsFiltered
indicates how many records are in the filtered result set. For example if you search the 6000 records and the search results in 2000 being displayed thenrecordsFiltered
should be 2000.The
data
object should contain only the number of records to be displayed on the page, ie 10, for the default page length of 10 rows.Your server script needs to understand the sent parameters in order to return the proper page worth of data. With server side processing your server script is responsible for searching and sorting.
Take a look at this example. Click on the
Ajax
tab to see what the response is when you perform searches and sorting. Also there is an example SSP script.You might be better off trying
deferRender
instead ofserverSide
processing. See this FAQ.Kevin