Apply filter by clicking on value in column
You can easily filter data in a table by clicking on a given value in cell, instead of opening filters and selecting value from there:
TextColumn::make('user.full_name') ->label('Person') ->tooltip('Filter by this person') ->disableClick() ->extraAttributes(function (Settlement $record) { return [ 'wire:click' => '$set("tableFilters.users.values", [' . $record->user_id . '])', 'class' => 'transition hover:text-primary-500 cursor-pointer', ]; }),
tableFilters.users.values - "users" is your filter name and depending on whether your filter allows multiple values to be selected values should be replaced by value and the field value should be passed as array or not.
If you need to set few filters at once, unfortunately you cannot set it inline, but instead you can extract it to dedicated method:
TextColumn::make('created_at') ->date() ->tooltip('Show only from this day') ->disableClick() ->extraAttributes(function ($record) { return [ 'wire:click' => 'filterFromDay("' . $record->created_at->format('Y-m-d') . '", "' . $record->created_at->format('Y-m-d') . '")', 'class' => 'transition hover:text-primary-500 cursor-pointer', ]; }),
and here is that method, which you should place in your ListRecords resource:
public function filterFromDay(string $from, string $until) : void{ $this->tableFilters['date_range']['created_from'] = $from; $this->tableFilters['date_range']['created_until'] = $until;}
Hi, The trick doesn't work since the quotes get escaped:
wire:click="$set('tableFilters.status.value', ' . open . ')"
Do you know how to fix this?
I dont't see your code, just the output, so it's hard to say, but I think maybe you're using double quotes instead of single. You can then try to escape them or extract method, the same as shown in the second example.
I copied your exact fragment, though I use a string as value:
The output results in this:
not only the status 'open' gets escaped, but the double quotes around
tableFilters.status.value
as well.BTW: my Filament version is v2.17.17.
It works, if you wrap the value for 'wire:click' in
extraAttributes
in anew HtmlString()
Thanks for the hint, after a few tries I got it to work like this:
With inverted quotes and the Dollar-sign in
$set
needs to be escapedWith suggestion of @Josef Behr I do like this and work. Thank all...
Thank you!
Nice trick! It works to change the url but does not reload the table. How can the filter be applied (table refresh)? (I'm using v3)