FilamentF
Filament15mo ago
Panos.S

Header Action filter not applying to Table Widget

Hello, I have created a custom page which extends the default Filament\Pages\Page and added a Header Action Filter Action filter like hoping to apply the filter on a custom table widget I have created. This setup works pretty well with chart widgets but this doesn't seem to be the case for table widgets.

This is the Page class:

class Commissions extends Page
{
    use HasFiltersAction, HasFiltersForm;

    protected static ?string $navigationIcon = 'heroicon-o-banknotes';

    protected static string $view = 'filament.pages.commissions';

    protected static ?string $navigationGroup = 'On-Site';

    public $year; // Declare year property to hold the selected year

    protected function getHeaderActions(): array
    {
        return [
            FilterAction::make()
                ->form([
                    Select::make('year')
                        ->options([
                            2024 => '2024',
                            2023 => '2023',
                            2022 => '2022',
                            'all' => 'All',
                        ])
                        ->native(false)
                        ->afterStateUpdated(function ($state){
                            $this->year = $state; // Set the selected year
                            $this->updateWidgets(); // Update the widgets with the new filter
                        }),
                ]),
        ];
    }

    protected function updateWidgets()
    {
        $this->dispatch('filtersUpdated', [
            'year' => $this->year, // Dispatch the year state when the filter is updated
        ]);
    }
}


This is the widget class:
class CommissionsTableChart extends BaseWidget
{
    use InteractsWithPageFilters;

    protected static ?string $pollingInterval = null;

    protected static ?string $heading = 'Commissions by Source'; // Default value for year

    protected static bool $isDiscovered = false;

    public $year;

    protected $listeners = ['filtersUpdated' => 'updateFilters'];

    public function updateFilters($filters): void
    {
        $this->year = $filters['year']; // Update the year from the filters
        $this->resetPage();
    }

    public function table(Table $table): Table
    {
        // Retrieve the year filter, defaulting to 'all' if not set
        $year = $this->year;
        
        return $table
            ->query(
                Registration::query()
                    ->when($year !== 'all' && ! empty($year), function (Builder $query) use ($year) {
                        $query->whereYear('reservation_date', '=', $year);
                    })


As you can see in the attached screenshare, only when I reclick the "filter" header action the page is actually refreshed with the year filtered.

Thanks a lot!
Was this page helpful?