Skip to content
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

Use column text for sorting and filter pills if no $filterNames or $sortNames exist #286

Merged
merged 3 commits into from
May 19, 2021
Merged

Use column text for sorting and filter pills if no $filterNames or $sortNames exist #286

merged 3 commits into from
May 19, 2021

Conversation

fabio-ivona
Copy link
Contributor

@fabio-ivona fabio-ivona commented May 17, 2021

This PR aims to define a better default value for filter/sorting pills, by giving precedence on the column text (if exists) over the column name of the model

let's see a use case: in non-english languages db column names often differs from what's displayed to the user, and this is greatly handled by laravel-livewire-tables:

public function columns(): array
{
    return [
        Column::make('Email', 'email')->sortable()->searchable(),
        Column::make('Nome', 'first_name')->sortable()->searchable(),
        Column::make('Cognome', 'last_name')->sortable()->searchable(),
        Column::make('', 'actions'),
    ];
}

but, for filtering and sorting I obtain this:

image

that's to say the human readable db column name: ucwords(strtr('first_name', ['_' => ' ', '-' => ' '])) // First Name

but, usually, I want the filter pills labels to be equal to the displayed column name:

image

I know this could solved by defining $filterNames and $sortNames properties, but that's kinda redundant.. as my labels are already written in columns definitions

so I think we can create a "pills label" priority such as

$sortNames[$col] > Column Text > Human Readable Db Column

this can be obtained by changing this:

{{ $sortNames[$col] ?? ucwords(strtr($col, ['_' => ' ', '-' => ' '])) }}

to this:

{{ $sortNames[$col] ?? collect($this->columns())->pluck('text', 'column')->get($col, ucwords(strtr($col, ['_' => ' ', '-' => ' ']))) }}

hope to have well explained the issue and the solution I'm trying to propose

a better solution would be extracting this logic in a method of WithFilter and WithSorting traits, but did not want to change so many lines without your opinion about this

as usual, thumbs up for your great work!

@rappasoft
Copy link
Owner

Very good thanks!

@rappasoft rappasoft added the Awaiting Next Release Currently merged into development awaiting a release to master label May 19, 2021
@rappasoft rappasoft mentioned this pull request May 19, 2021
@rappasoft rappasoft merged commit 36726a5 into rappasoft:master May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Next Release Currently merged into development awaiting a release to master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants