SelectFilter - can i provide my own value + label / remove the default option

SelectFilter always populates option without value and default label "All": <option>All</option> How can i provide my own value and label for it? Or second option - remove it altogether?
5 Replies
LeandroFerreira
you can change the label ->placeholder('new label') you can customize ->options() you can use a custom Select on ->forms() https://filamentphp.com/docs/3.x/tables/filters#custom-filter-forms
Gaspar
GasparOP2y ago
Well, none them actually solves the value problem, right? now: <option>All</<option> wish: <option value="0">All</option> Some backgound. I have hierarchical data (with parent IDs). And would like to (default) show only items with parentID = 0. If i use Filament Filter element, it has value null (and label/placeholder All which i can change) and it has no effect to the queried data (basically without filter). So my inital thought was to give this "placeholder" my own value (like i would do in basic HTML). And it should be default filter for my data. I also use this select element in form and it can not be null.
LeandroFerreira
none of them?
Filter::make('filter')
->form([
Select::make('parent')
->options([
0 => 'All',
1 => 'Option 1',
2 => 'Option 2',
])
->default(0)
->disablePlaceholderSelection() //v2
->selectablePlaceholder(false) //v3
])
->query(function (Builder $query, array $data): Builder {
return $query->whereParentId($data['parent']);
})
Filter::make('filter')
->form([
Select::make('parent')
->options([
0 => 'All',
1 => 'Option 1',
2 => 'Option 2',
])
->default(0)
->disablePlaceholderSelection() //v2
->selectablePlaceholder(false) //v3
])
->query(function (Builder $query, array $data): Builder {
return $query->whereParentId($data['parent']);
})
Gaspar
GasparOP2y ago
It seems the custom Form for this is a goto for me for now. I missed this selectablePlaceholder() part. Thank you! This would be amazing in the future that i could just pass(thru) this method from SelectFilter to Select element/object instead of building form for that same base element!
Tables\Filters\SelectFilter::make('parent')
->selectablePlaceholder(false)
Tables\Filters\SelectFilter::make('parent')
->selectablePlaceholder(false)
Ayman Alhattami
This work with me
->filters([
SelectFilter::make('currency_id')
->relationship('currency', 'name')
->searchable()
->default(Currency::default()->id)
->query(function (Builder $query, array $data): Builder {
return $query
->when(
optional($data)['value'],
fn (Builder $query, $date): Builder => $query->where('currency_id', optional($data)['value']),
);
}),
])
->filters([
SelectFilter::make('currency_id')
->relationship('currency', 'name')
->searchable()
->default(Currency::default()->id)
->query(function (Builder $query, array $data): Builder {
return $query
->when(
optional($data)['value'],
fn (Builder $query, $date): Builder => $query->where('currency_id', optional($data)['value']),
);
}),
])

Did you find this page helpful?