Friday, March 31, 2017

Descending order in yajra not working properly for second page onwards

For ascending sort , yajra is working properly(means response has properly sorted data). I have an array like this:

suppose i have :

$order = $order_data [0]['dir']
$column = $order_data[0]['column']
$length = 6;
$start = 6;

My Table datatable with id as ascending goes like this :

[
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"]
9=>["Id"=>"120", "name"=>"steve", "age"=>"34"]
10=>["Id"=>"121", "name"=>"elon", "age"=>"34"]
]

My back end implementation is this:

//$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');

I am getting response for test cases :

Test case : 2

Response: data:[]

order[0][column]:0
order[0][dir]:asc
start:10
length:10

Test case : 2

Response : data:[]

    order[0][column]:0
    order[0][dir]:desc
    start:10
    length:10

For temporary solution I am doing this :

//$results contains the results after sorting
$results = $model->getPeopleSortedById($start, $length, $order);

$dataForDT = new Collection();
$mDataSupport = 'true';
$orderFirst = 'false';

if ($order == "desc") {
        $orderFirst = 'false';
    }

if($order=="asc"){
       $temp =["Id"=>"", "name"=>"", "age"=>""];
} else {
       $temp =["Id"=>"999999999", "name"=>"zzzzzzzzzzz", "age"=>"99999999"];
}

$results = array_pad($results , (-((int)$start + count($results))), $temp);

foreach ($results as $ds) {
            $dataForDT->push($ds);
        }

return Datatables::of($dataForDT)
->with(['recordsTotal' => (int)$totalRecords, 'recordsFiltered' => 
(int)$totalRecords])
->make('true');

Now data is coming properly. Is it a bug of Yajra or what ?



via subhajit

Advertisement