how set default order ?
ex default order data column id
Follow this https://datatables.net/reference/option/order and just use the proper column index.
thanks
If you are using as a service, in the method html(), on parameters array use
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->parameters([
'order' => [
0, // here is the column number
'desc'
]
]);
Follow this https://datatables.net/reference/option/order and just use the proper column index.
This doesn't really solve a default order for the query though, for example the default sort that comes from an Eloquent model... if it is necessary to loop the first page to prevent a page jump, then the order will rearrange after the first AJAX call to sort
Follow this https://datatables.net/reference/option/order and just use the proper column index.
This doesn't really solve a default order for the query though, for example the default sort that comes from an Eloquent model... if it is necessary to loop the first page to prevent a page jump, then the order will rearrange after the first AJAX call to sort
Does this make sense? Is there a workaround or standard method? I'm still having a hard time finding this info
This idea is to have the $query
end up with the same defaults as the AJAX, for example to render the first page.
Here is a way to sort of hack in the functionality, but it does not seem ideal at all:
if (!request()->ajax() && !request()->get('order')) {
$query = $query->orderBy('column_name', 'desc');
}
$dataTable = DataTables::eloquent($query);
if (request()->ajax() && !request()->get('order')) {
$dataTable = $dataTable->order(function($query) {
$query->orderBy('column_name', 'desc');
});
}
The part before assigning $dataTable
handles the server side, and the part after handles the AJAX
There must be a more official way to do this?
Most helpful comment
If you are using as a service, in the method html(), on parameters array use