对于升序排序,yajra 工作正常(意味着响应具有正确排序的数据)。我有一个这样的数组:
假设我有:
$order = $order_data [0]['dir']
$column = $order_data[0]['column']
$length = 5;
$start = 5;
id 为升序的我的表数据表如下所示:
[
0=>["Id"=>"111", "name"=>"jack", "age"=>"23"]
1=>["Id"=>"112", "name"=>"harry", "age"=>"12"]
2=>["Id"=>"113", "name"=>"babu", "age"=>"23"]
3=>["Id"=>"114", "name"=>"ravi", "age"=>"43"]
4=>["Id"=>"115", "name"=>"suman", "age"=>"45"]
5=>["Id"=>"116", "name"=>"david", "age"=>"54"]
6=>["Id"=>"117", "name"=>"kiran", "age"=>"53"]
7=>["Id"=>"118", "name"=>"arjay", "age"=>"78"]
8=>["Id"=>"119", "name"=>"alien", "age"=>"23"]
]
我的后端实现是这样的:
//$results contains the results after sorting
$results = $model->getPeopleSortedById($start, $length, $order);
$dataForDT = new Collection();
foreach ($results as $ds) {
$dataForDT->push($ds);
}
return Datatables::of($dataForDT)
->with(['recordsTotal' => (int)$totalRecords, 'recordsFiltered' => (int)$totalRecords])
->make('true');
我收到了测试用例的回复:
测试用例:1
Response: data:[["Id"=>"116", "name"=>"david", "age"=>"54"],
["Id"=>"117", "name"=>"kiran", "age"=>"53"],
["Id"=>"118", "name"=>"arjay", "age"=>"78"],
["Id"=>"119", "name"=>"alien", "age"=>"23"]]
order[0][column]:0
order[0][dir]:asc
start:5
length:5
测试用例:2
Response : data:[] /* should have been [
["Id"=>"114", "name"=>"ravi", "age"=>"43"],
["Id"=>"113", "name"=>"babu", "age"=>"23"],
["Id"=>"112", "name"=>"harry", "age"=>"12"],
["Id"=>"111", "name"=>"jack", "age"=>"23"]] */
order[0][column]:0
order[0][dir]:desc
start:5
length:5
对于临时解决方案,我正在这样做:
//$results contains the results after sorting
$results = $model->getPeopleSortedById($start, $length, $order);
$dataForDT = new Collection();
$mDataSupport = 'true';
$orderFirst = 'false';
if ($order == "desc") {
$orderFirst = 'false';
}
//temporary fix start
if($order=="asc"){
$temp = ["Id"=>"", "name"=>"", "age"=>""];
} else {
$temp = ["Id"=>"999999999", "name"=>"zzzzzzzzzzz", "age"=>"99999999"];
}
$results = array_pad($results , (-((int)$start + count($results))), $temp);
//temporary fix end
foreach ($results as $ds) {
$dataForDT->push($ds);
}
return Datatables::of($dataForDT)
->with(['recordsTotal' => (int)$totalRecords, 'recordsFiltered' => (int)$totalRecords])
->make('true');
现在,使用粗线可以正确显示数据。从第二页开始,desc 的数据未显示。它是 Yajra 的错误吗?
PS:使用 Yajra v6.27.0。以前的版本中可能不存在此问题。