Select filter - remove/disable default option

I want to display records for one option only. I default the select filter to the first option but there is an 'All' option in the select filter which would allow a user to view all records. Is there any way to remove the "All" option?
9 Replies
awcodes
awcodes15mo ago
Filament
Getting started - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
رجل الجمبري
Sorry I'm not talking about pagination I'm talking about table filters. https://filamentphp.com/docs/2.x/tables/filters#select-filters I would essentially like the functionality of radio buttons but they don't appear to be an option.
Filament
Filters - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
awcodes
awcodes15mo ago
Well you can make a custom filter using a select that doesn’t have an all option. But all make sense as a way to clear the filter.
Kenneth Sese
Kenneth Sese15mo ago
I’m confused as to what you are trying to accomplish. Does just the normal filter that renders as a checkbox not accomplish what you want? What states do you want to have?
awcodes
awcodes15mo ago
I mean ‘all’ in a filter isn’t showing all records for the model. Its not pagination. It’s just resetting the filter to the original query.
Kenneth Sese
Kenneth Sese15mo ago
It sounds like OP just wants to have the records filtered by default? As I understand it, you have a select filter that has one option (let’s say “published) and you default to having it filter to “published” but it’s also showing “all” and you don’t want to allow your users to click “all”. That’s how I’m understanding the question. And if that’s the case you don’t need a filter at all. Just add a scope to your eloquent query. @max_prawn can you clarify?
رجل الجمبري
Sorry, I guess I didn't explain it well. @kennethsese you are on the right track. Lets say my options in the select filter are published and unpublished. I would set the default to published and only published records would be displayed. I want the user to be able to filter by unpublished records but never both published and unpublished.
Kenneth Sese
Kenneth Sese15mo ago
So in this case you'll have to do a custom filter like @awcodes said. The key is that when you do a custom filter and use a Select field you can disable the placeholder selection (something you can't do on Select Filters). So something like this:
Filter::make('gender')
->label('Gender')
->form([
Select::make('gender')
->disablePlaceholderSelection()
->default('male')
->options([
'male' => 'Male',
'female' => 'Female',
])
])
->indicateUsing(function (array $data): ?string {
return 'Gender: ' . $data['gender'];
})
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['gender'],
fn (Builder $query, $gender): Builder => $query->where('gender', $data['gender']),
);
}),
Filter::make('gender')
->label('Gender')
->form([
Select::make('gender')
->disablePlaceholderSelection()
->default('male')
->options([
'male' => 'Male',
'female' => 'Female',
])
])
->indicateUsing(function (array $data): ?string {
return 'Gender: ' . $data['gender'];
})
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['gender'],
fn (Builder $query, $gender): Builder => $query->where('gender', $data['gender']),
);
}),
رجل الجمبري
Thanks for your replies. I'll give this a go.