Php 从第二页开始,yajra中的降序顺序无法正常工作
对于升序排序,yajra工作正常(意味着响应对数据进行了正确排序)。我有这样一个数组: 假设我有:Php 从第二页开始,yajra中的降序顺序无法正常工作,php,mysql,laravel,yajra-datatable,Php,Mysql,Laravel,Yajra Datatable,对于升序排序,yajra工作正常(意味着响应对数据进行了正确排序)。我有这样一个数组: 假设我有: $order = $order_data [0]['dir'] $column = $order_data[0]['column'] $length = 5; $start = 5; id为升序的My Table datatable如下所示: [ 0=>["Id"=>"111", "name"=>"jack", "age"=>"23"] 1=>["Id"=>"
$order = $order_data [0]['dir']
$column = $order_data[0]['column']
$length = 5;
$start = 5;
id为升序的My Table datatable如下所示:
[
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');
//$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');
我的后端实现是这样的:
[
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');
//$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');
我收到了测试用例的响应:
[
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');
//$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');
测试用例: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
对于临时解决方案,我正在这样做:
[
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');
//$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');
现在,使用粗体线可以正确地获取数据。
描述第二页之后未显示数据。
这是一只雅吉拉虫子吗
PS:使用Yajra的v6.27.0。此问题在以前的版本中可能不存在