F
Filament3w ago
w5m

How to get all options from table filter in table header action?

I've created a custom filter form for a table on a relation page (i.e. a class which extends ManageRelatedRecords). I have a Select component as part of the filter form and this is populated with a list of projects. Within a table header action, I'd like to retrieve the list of projects (i.e. Select options), in order to provide navigation to one of the projects in the list. Unfortunately, I'm struggling to access the Select component, so would be grateful to be pointed in the right direction.
public function table(Table $table): Table
{
return $table
->columns([
// ...
])
->filters([
Filter::make('client_project')
->form([
Select::make('client_id'),
Select::make('project_id')
])
->query(function (Builder $query, array $data) {
// ...
}),
])
->headerActions([
Tables\Actions\Action::make('next-project')
->url(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();
dd($filters['client_project']);
// how do I retrieve all of the options listed in the 'project_id' Select component?
})
]);
}
public function table(Table $table): Table
{
return $table
->columns([
// ...
])
->filters([
Filter::make('client_project')
->form([
Select::make('client_id'),
Select::make('project_id')
])
->query(function (Builder $query, array $data) {
// ...
}),
])
->headerActions([
Tables\Actions\Action::make('next-project')
->url(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();
dd($filters['client_project']);
// how do I retrieve all of the options listed in the 'project_id' Select component?
})
]);
}
Solution:
I've finally managed to get hold of the list of Select options. I had to modify the creation of the project_id Select component slightly by adding ->key('project') . Then, in the header action, the following code gave me an array of the options... ```php Tables\Actions\Action::make('next-project') ->action(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();...
Jump to solution
5 Replies
ChesterS
ChesterS3w ago
What are you trying to achieve?
Tables\Actions\Action::make('next-project')
->action(function (\Livewire\Component $livewire): string { // Changed to 'action' instead of
$filters = $livewire->getTable()->getFilters();
dd($filters['client_project']->getState());
})
Tables\Actions\Action::make('next-project')
->action(function (\Livewire\Component $livewire): string { // Changed to 'action' instead of
$filters = $livewire->getTable()->getFilters();
dd($filters['client_project']->getState());
})
The above should give you the filter values.
w5m
w5m3w ago
I'm trying to get the full list of options from the project_id Select component (not the currently selected option, which ->getState() returns), so that I can decide which id from the list should be used to build a URL to navigate to. I'm guessing I need to use the ->getOptions() method, but I'm not sure how I get access to the Select component. I was trying to avoid executing another database query, as the list of relevant project id's already exists in the Select component's list of options.
ChesterS
ChesterS3w ago
Hmm why not save the options in a variable?
public function table(Table $table): Table
{
$options = Model::all();
return $table
->filters(...) // user your $options here
->headerActions()
public function table(Table $table): Table
{
$options = Model::all();
return $table
->filters(...) // user your $options here
->headerActions()
I guess it gets more complicated if you have a dynamic select 🤔 But there should be ways to avoid running it again
w5m
w5m3w ago
Unfortunately, the client_id and project_id components are dependent Selects, so the latter is dynamically populated based on the former...
Select::make('project_id')
->relationship('project', 'number', modifyQueryUsing: function (Builder $query, Get $get) {
return $query
->where('client_id', $get('client_id'))
->orderBy('number', 'desc');
})
Select::make('project_id')
->relationship('project', 'number', modifyQueryUsing: function (Builder $query, Get $get) {
return $query
->where('client_id', $get('client_id'))
->orderBy('number', 'desc');
})
Solution
w5m
w5m3w ago
I've finally managed to get hold of the list of Select options. I had to modify the creation of the project_id Select component slightly by adding ->key('project') . Then, in the header action, the following code gave me an array of the options...
Tables\Actions\Action::make('next-project')
->action(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();
$filter = $filters['client_project'];
dd($filter->getForm()->getComponent('project')->getOptions());
})
Tables\Actions\Action::make('next-project')
->action(function (\Livewire\Component $livewire): string {
$filters = $livewire->getTable()->getFilters();
$filter = $filters['client_project'];
dd($filter->getForm()->getComponent('project')->getOptions());
})
Want results from more Discord servers?
Add your server