Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 431
Hamza Dhamiya Avatar asked Jan 18 '26 11:01

Hamza Dhamiya


2 Answers

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]}];
like image 51
Pratik Powar Avatar answered Jan 21 '26 05:01

Pratik Powar


I stumble recently on this issue and I found out that on the latest version of Yajra dataTables the DT_Row_Index name has changed.

Solution:

change DT_Row_Index to DT_RowIndex

reference: https://yajrabox.com/docs/laravel-datatables/master/index-column

like image 42
Dards Avatar answered Jan 21 '26 04:01

Dards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!