phplaraveldatatablesyajra-datatable

DT_Row_Index not found in Yajra Datatable


I am using Yajra Datatables, this is my php code

$quotes=User::find($id)->quotes();
        return Datatables::of($quotes)
            ->addIndexColumn()
            ->setRowClass(function($quote){
                return $quote->quote_urgent?"table-primary":"";
            })
            ->addColumn("convert","Convert")
            ->addColumn("action",function($quote){
                return "<a href='".URL::to("customer/".$quote->quote_customer_id."/view-quote/".$quote->quote_id)."'>Details</a>";
            })
            ->make(true);

and my javascript is

$(document).ready(function() {
                    $('.datatable').DataTable({
                        processing: true,
                        serverSide: true,
                        buttons:[
                            {extend:'paginate_button',className:'btn btn-primary'}
                        ],
                        pageLength:4000,
                        ajax: '{{ route('CustomerQuoteRecords',$customer->id) }}',
                        columns:[
                            {"data":"DT_Row_Index"},
                            {"data":"quote_id"},
                            {"data":"created_at"},
                            {"data":"quote_received","defaultContent":"-"},
                            {"data":"quote_name"},
                            {"data":"quote_price"},
                            {"data":"convert"},
                            {"data":"action"},
                        ]
                    });
                });

The above code gives error of DT_Row_Index column not found because by default first column have order applied, as we know addIndexColumn adds extra data to the returned JSON but DT_Row_Index is not present in the database table.

But when I change it to this, it works correctly.

columns:[
                            {"data":"quote_id"},
                            {"data":"DT_Row_Index"},
                            {"data":"created_at"},
                            {"data":"quote_received","defaultContent":"-"},
                            {"data":"quote_name"},
                            {"data":"quote_price"},
                            {"data":"convert"},
                            {"data":"action"},
                        ]

But I need DT_Row_Index in first column.


Solution

  • This is because data-table tries to find DT_Row_Index for searching and sorting. If you added this DT_Row_Index then you should disable searching and sorting for this column. if you don't want searching and sorting then this code should help you. Add this code to your JavaScript at data-table initialization.

     aoColumnDefs = [{'bSortable': false, 'aTargets': [0]},{'bSearchable': false, 'aTargets': [0]}];