-
-
Notifications
You must be signed in to change notification settings - Fork 63
Open
Description
Hi,
By using the DataTables Html Plugin, we can easily declare our columns in getColumns()
method, but if we need to apply custom filters or edit, we must do it in the dataTable()
method.
class UsersDataTable extends DataTable
{
public function getColumns(): array
{
return [
Column::make('id'),
Column::make('name'),
Column::make('email'),
Column::make('created_at'),
Column::make('updated_at'),
];
}
public function dataTable(QueryBuilder $query): EloquentDataTable
{
return (new EloquentDataTable($query))
->setRowId('id')
->filterColumn('name', function ($query, $keyword) {
$query->where(...);
})
->editColumn('name', function ($user) {
return $user->firstName.' '.$user->lastName;
});
}
}
When we have a lot of columns, it can be a bit confusing and not easy to manage.
We need to declare columns in 1 place and filter/edit then in another place.
I would like to suggest to implement new methods on the Column
class to automatically register edit
and filter
in the datatable.
The result will look like this:
class UsersDataTable extends DataTable
{
public function getColumns(): array
{
return [
Column::make('id'),
Column::make('name')
->filter(function ($query, $keyword) {
$query->where(...);
})
->edit(function ($user) {
return $user->firstName.' '.$user->lastName;
}),
Column::make('email'),
Column::make('created_at'),
Column::make('updated_at'),
];
}
public function dataTable(QueryBuilder $query): EloquentDataTable
{
return (new EloquentDataTable($query))->setRowId('id');
}
}
What do you think?
obyajra
Metadata
Metadata
Assignees
Labels
No labels