doukong9982 2014-08-26 19:51
浏览 57

DataTables不应用每页过滤

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.

  • 写回答

1条回答 默认 最新

  • doucang9673 2014-08-26 20:14
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么