Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Seb33300 opened this issue May 26, 2025 · 2 comments
Open

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

Seb33300 opened this issue May 26, 2025 · 2 comments

Comments

@Seb33300
Copy link
Contributor

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?

Repository owner deleted a comment from obyajra May 26, 2025
@yajra
Copy link
Owner

yajra commented May 26, 2025

I love the idea since this is what I am also doing now with the export functionalities via exportFormat and exportRender.

And I think we should also consider the orderColumn callback?

For edit on the other hand, I tend to use render, renderRaw, and delegate it to js/client side when possible. I think it's better to make the server-side response as small as possible for better performance.

I agree with your idea of adding these functionalities to the Column definition.

Thanks!

@Seb33300
Copy link
Contributor Author

Yes, I forgot the orderColumn but yes, it should be included too.

I also try to use render, renderRaw when possible, but in some cases it can be more convenient to use editColumn.
That's why I would like to support it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants