SelectFilter hidden()

So I have this SelectFilter that I want hidden based on $activeTab that is set by getTabs(). I have
SelectFilter::make('status')
->label('Status')
->hidden(function () {
ray($this->activeTab)->label('status');

return in_array($this->activeTab, ['all', 'active', 'inactive']);
})
->options([
'Active' => 'Active',
'Prospect' => auth()->user()->isInternal() ? 'Prospect' : 'Draft',
'Inactive' => 'Inactive',
])->query(function (Builder $query, array $data): Builder {
$value = $data['value'] ?? null;

return match ($value) {
'Active' => $query->where('active', 1),
'Prospect' => $query->where('active', 0)->where('draft', 1),
'Inactive' => $query->where('active', 0)->where('draft', 0),
default => $query,
};
}),
SelectFilter::make('status')
->label('Status')
->hidden(function () {
ray($this->activeTab)->label('status');

return in_array($this->activeTab, ['all', 'active', 'inactive']);
})
->options([
'Active' => 'Active',
'Prospect' => auth()->user()->isInternal() ? 'Prospect' : 'Draft',
'Inactive' => 'Inactive',
])->query(function (Builder $query, array $data): Builder {
$value = $data['value'] ?? null;

return match ($value) {
'Active' => $query->where('active', 1),
'Prospect' => $query->where('active', 0)->where('draft', 1),
'Inactive' => $query->where('active', 0)->where('draft', 0),
default => $query,
};
}),
What I am finding odd is that while ray() is showing a matching activeTab value, its not hidding on first ocurrence when switching to one of the tabs that it should be hidden on even though ray shows a value that would be true. The in_array() is returning the proper true/false too. Suggestions? I had this issue with toggleable columns at one point and i started doing a $this->resetPage() when a tab was changed and that solved it, but this appears to fire before the filter hidden() logic is ran. Also, the hidden() method is seeing the right values, so thats not it.
Solution:
@archilex this is working great. thanks! ```php ->hidden(function () { $tabsToHideFilter = ['active', 'inactive', 'draft']; $shouldHideFilter = in_array($this->activeTab, $tabsToHideFilter); ...
Jump to solution
10 Replies
Kenneth Sese
Kenneth Sese15mo ago
@Mark Chaney Hi. It's because the form is being cached and it's pulling the cached form when you click the tab. You see the caching in the getTableFiltersForm method in HasFilters . One option would be to unset the cachedForm after clicking on the tab: unset($this->cachedForms['tableFiltersForm']) to force it to re-render the form.
Mark Chaney
Mark ChaneyOP15mo ago
@archilex awesome. thanks! While I have your attention, how about unsetting the filter on certain tabs? 😛 once we are on the panel and out of alpha, I really just need to use your plugin
Kenneth Sese
Kenneth Sese15mo ago
There's a removeTableFilters() and removeTableFilter() method you can probably tap into depending on what you need. Advanced Tables v3 (formerly #filter-sets) is out now. yay!!! I'm waiting for the plugin to update on Filament's site to start promoting, change discord name, etc.
Mark Chaney
Mark ChaneyOP15mo ago
pff, didn't go for Table Builder Plus? 😛
Kenneth Sese
Kenneth Sese15mo ago
haha...I like that Advanced starts with an "A"! Pretty much the only reason. I didn't want to go further down the alphabet
Solution
Mark Chaney
Mark Chaney15mo ago
@archilex this is working great. thanks!
->hidden(function () {
$tabsToHideFilter = ['active', 'inactive', 'draft'];
$shouldHideFilter = in_array($this->activeTab, $tabsToHideFilter);

if ($shouldHideFilter) {
$this->tableFilters['status'] = null;
}

return $shouldHideFilter;
})
->hidden(function () {
$tabsToHideFilter = ['active', 'inactive', 'draft'];
$shouldHideFilter = in_array($this->activeTab, $tabsToHideFilter);

if ($shouldHideFilter) {
$this->tableFilters['status'] = null;
}

return $shouldHideFilter;
})
public function updatedActiveTab(): void
{
unset($this->cachedForms['tableFiltersForm']);
$this->resetPage();
}
public function updatedActiveTab(): void
{
unset($this->cachedForms['tableFiltersForm']);
$this->resetPage();
}
Mark Chaney
Mark ChaneyOP15mo ago
had to move cache unset to hook. wasnt working reliably from filter. not a huge fan, but not a big deal. Didnt really want to unset the cache unless it was needed to do, but now it happens on every tab change. oh well
Kenneth Sese
Kenneth Sese15mo ago
Can you do a conditional?
public function updatedActiveTab($value): void
{
if ($value === 'foo') {
unset($this->cachedForms['tableFiltersForm']);
}

$this->resetPage();
}
public function updatedActiveTab($value): void
{
if ($value === 'foo') {
unset($this->cachedForms['tableFiltersForm']);
}

$this->resetPage();
}
Mark Chaney
Mark ChaneyOP15mo ago
@kennethsese that method is on my FilterTableTabs trait
Kenneth Sese
Kenneth Sese15mo ago
Or...
public function updatedActiveTab(): void
{
if ($this->activeTab === 'foo') {
unset($this->cachedForms['tableFiltersForm']);
}

$this->resetPage();
}
public function updatedActiveTab(): void
{
if ($this->activeTab === 'foo') {
unset($this->cachedForms['tableFiltersForm']);
}

$this->resetPage();
}
Want results from more Discord servers?
Add your server