Laravel-datatables: how set default order

Created on 2 Feb 2016  ·  6Comments  ·  Source: yajra/laravel-datatables

how set default order ?
ex default order data column id

Most helpful comment

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'
                ]
           ]);

All 6 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shadoWalker89 picture shadoWalker89  ·  3Comments

alejandri picture alejandri  ·  3Comments

FilipeBorges1993 picture FilipeBorges1993  ·  3Comments

kamrava picture kamrava  ·  3Comments

ahmadbadpey picture ahmadbadpey  ·  3Comments