Andrew
Andrew

Reputation: 4441

DataTables Not Applying per page filtering

I'm attempting to use DataTables Server Side Processing

Currently I'm getting 1000 rows back (as display) even though the display is set to 10, 25, etc

jQuery:

$(document).ready(function() {
    $('#paginatedTableSS').dataTable( {
      "processing": true,
      "serverSide": true,
      "ajax": "script.php"
      } );
} );

script.php:

$i = 0;
$test = array();
$test['draw'] = 1;
$test['recordsTotal'] = 200000;
$test['recordsFiltered'] = 8;
$test['data'] = array();
while($i<=20){
  array_push($test['data'], ['test1 ' . $i, 'test2 ' . $i, 'test3 ' . $i, 'test4 ' . $i]);
  $i=$i+1;
}
echo json_encode($test);

My ultimate goal is to pass along start to the script so the script can use that to give the correct data back. But currently I need to fix the above before moving forward I think. But I'm not sure what's wrong with it or how to debug it.

Upvotes: 0

Views: 1213

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32402

aaData seems to indicate that you're using the legacy datatables. If that's the case, you're missing iTotalRecords, iTotalDisplayRecords and sEcho.

$test['iTotalRecords'] = count($test['aaData']);
$test['iTotalDisplayRecords'] = count($test['aaData']);
$test['Echo'] = $_REQUEST['sEcho'];

see http://legacy.datatables.net/usage/server-side

if you're not using the legacy datatables you need to use data, records and recordsFiltered instead (as referenced in your link)

Edit

The reason your table is displaying 1000 rows is because that's the # of rows you're returning. Use the length and start parameters to determine which rows to return i.e

$start = (isset($_REQUEST['start']) && is_numeric($_REQUEST['start']))
    ? $_REQUEST['start'] : 0;

$length = (isset($_REQUEST['length']) && is_numeric($_REQUEST['length']))
    ? $_REQUEST['length'] : 100;

$test = array('data'=>array(), 'recordsTotal', 'draw', 'recordsFiltered');

for($i=$start; $i<=$length; $i++) {
  array_push($test['data'], array('id'=>$i,'date'=>$i,'status'=>$i,'options'=>$i));  
}

$test['recordsTotal'] = 200000;
$test['draw'] = 1;
$test['recordsFiltered'] = count($test['data']);
echo json_encode($test);

Upvotes: 1

Related Questions