F
Filament12mo ago
Nate

Is there a way to intercept the activeTab parameter in the resource table function?

I want to change the table structure depending on the tab that was just clicked. I can get the value after you leave the tab using $table->getLivewire()->activeTab (as the tab is probably only available after the table has rendered). I would like to do something like:
public static function table(Table $table): Table
{
if ($activeTab === 'Tab One') {
return (new TableConfigurationOne($table));
} // and more...

return (new TableConfigurationDefault($table));
}
public static function table(Table $table): Table
{
if ($activeTab === 'Tab One') {
return (new TableConfigurationOne($table));
} // and more...

return (new TableConfigurationDefault($table));
}
TIA
8 Replies
LeandroFerreira
LeandroFerreira12mo ago
Would you like to show/hide columns on a specific tab?
TextColumn::make('column')
->hidden(fn ($livewire) => $livewire->activeTab === 'Tab One')
TextColumn::make('column')
->hidden(fn ($livewire) => $livewire->activeTab === 'Tab One')
Nate
NateOP12mo ago
It may be enough wish you could do $table->hidden(). Let me see if the above is enough. Thanks.
Ola A
Ola A12mo ago
No. It isn't. We want to swap out the entire table because the columns to be displayed are different. Different table headers, different parent eloquent query. Also, it is cleaner to not have to load all that data if the view isn't using it.
Nate
NateOP12mo ago
@Ola A @Leandro Ferreira Yeah the problem is this doesn't change the underlying query. Be better if we can pull in a different table. Ok I have found a "hacky" way around this. I overrode the vendor/filament/components/tabs/item.blade.php so I could add a click event (there is already a wire:click on there but it just sets and not dispatched).
@click="$dispatch('tabChange', { tab: '{{ $slot->toHtml() }}' })"
@click="$dispatch('tabChange', { tab: '{{ $slot->toHtml() }}' })"
I then listen for the event in the list page like so:
#[On('tabChange')]
public function tabChange(): void
{
$this->resetTable();
}
#[On('tabChange')]
public function tabChange(): void
{
$this->resetTable();
}
This refreshed the table again so you can get the current tab value (without it you can only get the previous tab value as the page doesn't refresh between tab changes). I now have access to the current active tab and can then switch tables based on this.
public function table(Table $table): Table
{
if ($table->getLivewire()->activeTab === 'Dispatch certificates') {
return $this->batchedTable($table);
}

return $this->defaultTable($table);
}
public function table(Table $table): Table
{
if ($table->getLivewire()->activeTab === 'Dispatch certificates') {
return $this->batchedTable($table);
}

return $this->defaultTable($table);
}
shipit
epertinez
epertinez6mo ago
I had <x-filament::tabs.item :active="$activeTab === 'termes'" wire:click="$set('activeTab', 'termes')" >Termes</x-filament::tabs.item> But I don't know how to listen for $set, so $set was called AFTER table($table) and $this->activeTab had the old incorrect tab. If somebody can tell me what function do I have to create to listen $set('somevar','somevalue') I would appreciate. So far I tried updateSomevar($somevalue), but it does not work. Anyway, I ended up changing the calls: <x-filament::tabs.item :active="$activeTab === 'frases'" wire:click="setTabTo('frases')">Frases</x-filament::tabs.item> This way I can create a function in my component: public function setTabTo($tab) { $this->activeTab=$tab; $this->resetTable(); return; } That changes the variable and then resets the table. The table function is called twice, as table is called before seting the variable, but it seems to work.
LeandroFerreira
LeandroFerreira6mo ago
did you try updatedActiveTab() in the ListPage?
epertinez
epertinez6mo ago
GREAT, that works. So I only need to create public function updatedActiveTab() { // It tells me this parent function does not exists. parent::updatedActiveTab(); $this->resetTable(); } And this way table gets called twice, but the second time $this->activeTab holds the correct value. Thanks!!
Want results from more Discord servers?
Add your server