Deactivate filters when one filter activates

Hey - following scenario: Given I got Filter A, B and C. C is a toggle-filter and I want Filter A+B deactivated as soon as C is activated. How do I achieve this? Thanks. 🙂
10 Replies
Patrick Boivin
@husky110 Can you share the code of your filters?
Husky110
Husky110OP2y ago
yes - but gotta hide some of it. For context -> we're in a resource. I want hat filter "fast_open" deactivated any other filters when it's activated.
Husky110
Husky110OP2y ago
Husky110
Husky110OP2y ago
Do not get confused by the m__ - I'm using my own translation-plugin (which I might release as a package later).
Patrick Boivin
I think you'll need to extend the Filter class and override the getFormField() method. Maybe something like this:
protected function getFormField(): Field
{
return parent::getFormField()
->afterStateUpdated(function ($state, $livewire) {
if ($state) {
$livewire->tableFilters['something_delivery_status_id']['value'] = null;
}
});
}
protected function getFormField(): Field
{
return parent::getFormField()
->afterStateUpdated(function ($state, $livewire) {
if ($state) {
$livewire->tableFilters['something_delivery_status_id']['value'] = null;
}
});
}
Kenneth Sese
Kenneth Sese2y ago
You should be able to hide them with the hidden() or visible() method just like you would any other form component. Is that not working? You can just check against the tableFilter array hidden(fn ($livewire) => $livewire->tableFilters…. dd($livewire->tableFilters) to see the filter array when you have a filter turned on Filters are reactive by default so you don’t need to add reactive().
Husky110
Husky110OP2y ago
Huh... So basically we have two possible ways here... I think I will go with @pboivin's solution here, cause I think I will have to use it multiple times and just making a "KillerFilter" or sth. along those lines is better in readability and useability than having to rewrite the function multiple times. If it were only one, @kennethsese's solution would make sense tho. Thank you guys. 🙂
Patrick Boivin
Yeah, I think re-setting vs. hiding filters are two valid use-cases, you have a few options there 👌
Kenneth Sese
Kenneth Sese2y ago
filterKiller() is a great method name!!! 😂
Husky110
Husky110OP2y ago
it's class KillerFilter tho... 😄 Yep - which is why I try to state it as clearly as possible, so if someone finds this thread via searching, they get the full picture and hopefully a good solution to their problem. 🙂 If anyone needs it - here's the template:
<?php

namespace App\Filament\CustomFilters;

use Filament\Forms\Components\Field;
use Filament\Tables\Filters\Filter;

class KillerFilter extends Filter
{
protected function getFormField(): Field
{
return parent::getFormField()
->afterStateUpdated(
function ($state, &$livewire){
if($state){
$livewire->tableFilters = [
$this->getName() => [
'isActive' => true
]
];
}
}
);
}
}
<?php

namespace App\Filament\CustomFilters;

use Filament\Forms\Components\Field;
use Filament\Tables\Filters\Filter;

class KillerFilter extends Filter
{
protected function getFormField(): Field
{
return parent::getFormField()
->afterStateUpdated(
function ($state, &$livewire){
if($state){
$livewire->tableFilters = [
$this->getName() => [
'isActive' => true
]
];
}
}
);
}
}
Thanks @pboivin and @kennethsese Had to update my template:
<?php

namespace App\Filament\CustomFilters;

use Filament\Forms\Components\Field;
use Filament\Tables\Filters\Filter;

class KillerFilter extends Filter
{
protected function getFormField(): Field
{
return parent::getFormField()
->afterStateUpdated(
function ($state, &$livewire){
if($state){
foreach ($livewire->tableFilters as $key => $filter){
if($key == $this->getName()){
$livewire->tableFilters[$key] = [
'isActive' => true
];
} else {
$livewire->tableFilters[$key]['values'] = null;
$livewire->tableFilters[$key]['isActive'] = false;
}
}
}
}
);
}
}
<?php

namespace App\Filament\CustomFilters;

use Filament\Forms\Components\Field;
use Filament\Tables\Filters\Filter;

class KillerFilter extends Filter
{
protected function getFormField(): Field
{
return parent::getFormField()
->afterStateUpdated(
function ($state, &$livewire){
if($state){
foreach ($livewire->tableFilters as $key => $filter){
if($key == $this->getName()){
$livewire->tableFilters[$key] = [
'isActive' => true
];
} else {
$livewire->tableFilters[$key]['values'] = null;
$livewire->tableFilters[$key]['isActive'] = false;
}
}
}
}
);
}
}
The previous template produced failures when there is more than one KillerFilter present.
Want results from more Discord servers?
Add your server