F
Filamentβ€’4mo ago
ImShehryar

Multi-select select all/deselect all

Is there any better solution to select all/deselect all options then the following, needs help to refactor the toggeling
Select::make('departments')
->required()
->multiple()
->searchable()
->preload()
->relationship(titleAttribute: 'name')
->hintActions([
Action::make('select_all')
->visible(fn($state) => empty($state))
->action(fn (Select $component) => $component->state(Department::pluck('id')->toArray())),

Action::make('deselect_all')
->hidden(fn($state) => empty($state))
->action(function (Set $set) {
$set('departments', null);
}),
])
Select::make('departments')
->required()
->multiple()
->searchable()
->preload()
->relationship(titleAttribute: 'name')
->hintActions([
Action::make('select_all')
->visible(fn($state) => empty($state))
->action(fn (Select $component) => $component->state(Department::pluck('id')->toArray())),

Action::make('deselect_all')
->hidden(fn($state) => empty($state))
->action(function (Set $set) {
$set('departments', null);
}),
])
3 Replies
krekas
krekasβ€’4mo ago
I think you should be able to get options from the component then you won't need the db query maybe $component->getOptions() or something similar
ImShehryar
ImShehryarOPβ€’4mo ago
Neither of these working when click on 'select all'
->action(fn (Select $component) => $component->getOptions())

->action(fn (Select $component) => array_keys($component>getOptions()))

->action(fn (Select $component) => array_values($component->getOptions()))
->action(fn (Select $component) => $component->getOptions())

->action(fn (Select $component) => array_keys($component>getOptions()))

->action(fn (Select $component) => array_values($component->getOptions()))
Sorry, this works:
->action(fn (Select $component) => $component>state(array_keys($component->getOptions())))
->action(fn (Select $component) => $component>state(array_keys($component->getOptions())))
LeandroFerreira
LeandroFerreiraβ€’4mo ago
Sandro Gehri (@gehrisandro) on X
πŸ’‘ Filament tip @filamentphp hintAction() makes it really easy to add a "Select all" action to your select elements. Code in thread πŸ‘‡
From An unknown user
Twitter
Want results from more Discord servers?
Add your server