Skip to content

Support editColumn() and filterColumn() on Column #239

@Seb33300

Description

@Seb33300

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions