Laravel Yajra DataTables default sorting with condition

173 Views Asked by At

i have an datatables using yajra, and i have the column named "status". "status" columns contains -2 : Rejected By Finance, -1 : Rejected By Marketing, 0 : Pending, 1 : Accepted By Marketing, 2 : Accepted By Finance.

so i want to default sort by my tables ASCENDING where status > 0 but yajra datatables doesn't support order by in the server side. How can i reach that condition. Thanks

var table = $('#example').DataTable({
            processing: true,
            serverSide: true,
            order: [[1,'desc']],
            ajax: {
                url: "{{ route('voucher.data') }}",
                data:function (d) {
                    d.merchant = merchant? merchant : '';
                    d.status = status? status : '';
                    d.daterange = daterange? daterange : '';
                },
                type:'POST',
            },
            columns: [
              {data: 'id', name: 'id'},
              {data: 'requested_date', name: 'requested_date'},
              {data: 'merchant.nama', name: 'merchant.nama'},
              {data: 'total', name: 'total'},
              {data: 'status', name: 'status', className: "text- 
               center"},
              {data: 'action', name: 'action', className: "text- 
              center", visible: approvalAccess, orderable:false, 
              sortable:false},
            ]
        });

Yajra Datatables Laravel Order By with Condition.

1

There are 1 best solutions below

0
On

You've to put correct column for ordering, currently you're ordering order: [[1,'desc']] which need to be [[4,'asc']].

var table = $('#example').DataTable({
            processing: true,
            serverSide: true,
            order: [[4,'asc']],
            ajax: {
                url: "{{ route('voucher.data') }}",
                data:function (d) {
                    d.merchant = merchant? merchant : '';
                    d.status = status? status : '';
                    d.daterange = daterange? daterange : '';
                },
                type:'POST',
            },
            columns: [
              {data: 'id', name: 'id'},
              {data: 'requested_date', name: 'requested_date'},
              {data: 'merchant.nama', name: 'merchant.nama'},
              {data: 'total', name: 'total'},
              {data: 'status', name: 'status', className: "text- 
               center"},
              {data: 'action', name: 'action', className: "text- 
              center", visible: approvalAccess, orderable:false, 
              sortable:false},
            ]
        });

Also you can order by server side: https://yajrabox.com/docs/laravel-datatables/master/order-column