I have a problem defining DT_RowIndex using the file datatable yajra on laravel 9, i don't want to use the id of the table. iwant to defining number of row with pagination table on the datatable. this is the file on datatable
<?php
namespace App\DataTables;
use App\Models\User;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;
class UsersDataTable extends DataTable
{
public function dataTable($query)
{
return datatables()
->eloquent($query)
->editColumn('actions', function ($data) {
return "<a href=".route('users.edit', $data->id) . " class='btn btn-primary btn-sm'>
Edit
</a> <button class='delete btn btn-danger btn-sm' onclick='deleteFunc(". $data->id .")'> Delete </button>";
})
->rawColumns(['actions']);
}
public function query(User $model)
{
return $this->applyScopes($model->query());
}
public function html()
{
return $this->builder()
->setTableId('usersdatatable-table')
->columns($this->getColumns())
->minifiedAjax()
->dom('Bfrtip')
->orderBy(1)
->buttons(
Button::make('create'),
Button::make('export'),
Button::make('print'),
Button::make('reset'),
Button::make('reload')
);
}
protected function getColumns()
{
return [
Column::make('no')
->addIndexColumn(true),
Column::make('name'),
Column::computed('actions')
->exportable(false)
->printable(false)
->width(200)
->addClass('text-center'),
];
}
protected function filename() : string
{
return 'Users_' . date('YmdHis');
}
}
so How to defining the code of DT_RowIndex on that's file?
You can add an index column on your response by using addIndexColumn
API/method
based on your code:
public function dataTable($query)
{
return datatables()
->eloquent($query)
->addIndexColumn()
->editColumn('actions', function ($data) {
return "<a href=".route('users.edit', $data->id) . " class='btn btn-primary btn-sm'>
Edit
</a> <button class='delete btn btn-danger btn-sm' onclick='deleteFunc(". $data->id .")'> Delete </button>";
})
->rawColumns(['actions']);
}
Using addIndexColumn
will add another column on your response with a column name that is set on index_column
configuration. The default index column name is DT_RowIndex
.
reference: https://yajrabox.com/docs/laravel-datatables/master/index-column