#filtersForm with action button

13 messages · Page 1 of 1 (latest)

daring cradle
#

Hello,

i create a simple report page with filter form.

The filters work properly but how do I update the filters with an action button instead of the simple change event?
And How do i create a reset button that reset all filters?

public function filtersForm(Form $form): Form
    {
        return $form
            ->schema([
                Section::make()
                    ->schema([
                        Select::make('group_id_filter')               
                            ->options(Group::all()
                                ->pluck('descrizione', 'id'))
                            ->multiple()
                            ->searchable(),
                        Select::make('activity_id_filter')
                            ->options(Activity::query()
                                ->where('tipo','gaming') // ToDo Usare gaming o classifica???
                                ->pluck('titolo', 'id'))
                            ->multiple()
                            ->searchable()
                    ])
                    ->columns(3),
            ]);
    }
fallow valleyBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

thorn ermine
#

You can Defer the Filter which will enable you to customize the the apply filter action, referr to documentation

daring cradle
# thorn ermine https://filamentphp.com/docs/3.x/tables/filters/getting-started#deferring-filter...

i don't use table filter,
but filtersForm, next inject filter to table query

<?php

namespace App\Filament\Widgets;

use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Actions\ExportAction;

use Filament\Widgets\TableWidget as BaseWidget;

use Filament\Widgets\Concerns\InteractsWithPageFilters;

use App\Models\UserActivity;
use App\Models\Group;
use App\Models\Activity;

use Illuminate\Support\Facades\DB;

use App\Filament\Exports\GroupsReportExporter;


class GroupsReportTable extends BaseWidget
{

    use InteractsWithPageFilters;

    public function table(Table $table): Table
    {

        $group_id_filter = $this->filters['group_id_filter'] ?? null;
        $activity_id_filter = $this->filters['activity_id_filter'] ?? null;

        if($group_id_filter == null) {
            $group_id_filter = Group::all()->pluck('id')->toArray();
        }

        if($activity_id_filter == null) {
            $activity_id_filter = Activity::all()->pluck('id')->toArray(); 
        }

        return $table
            ->query(Group::query()
                ->select("groups.*",
                         DB::raw("get_group_points(groups.id, '".implode(",", $activity_id_filter)."') AS point")
                        )
                ->whereIn("id", $group_id_filter)
            )
            ->columns([
                TextColumn::make('desc'),
                TextColumn::make('point'),
            ]);
    }
}
daring cradle
#

any suggestions?

thorn ermine
#

Its by design, the data between the table and the form is always live to refresh the widget, you can swap it with Action Modal, even in the docs it explain it refer to this

#
The filters do not update the widgets until the user clicks the "Apply" button, which means that the widgets are not reloaded until the user is ready. This can improve performance if the widgets are expensive to load.
#

also, using the filters form do not do validation on the form refer to this

#
The $this->filters array will always reflect the current form data. Please note that this data is not validated, as it is available live and not intended to be used for anything other than querying the database. You must ensure that the data is valid before using it. In this example, we check if the start date is set before using it in the query.
#

So using Action Modal will give you the benefit of validating the data on the form before reaching the database and also the filter will not be applied unti the user clicks on the apply button

#

Hope that helps