Page Filters with InfoLists not working

I have a lot going on here and not too sure whats causing what. So currently happening: I can change a filter and the response will update, but if I remove the filter back to "select a x". The data goes blank, I want this to reset that filter and display all data as normal (well, minus that filter I just removed). Is it possible to get the table style filters instead, that may be easier?
1 Reply
Jamie Cee
Jamie Cee4mo ago
My class...
class Report extends Page implements HasForms, HasInfolists
{
use InteractsWithForms, InteractsWithInfolists, HasFiltersForm, WithPagination;
use InteractsWithPageFilters;

protected $data = [];
public int | string $perPage = 2;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.report';

protected static ?string $title = 'Reports';

protected static ?string $slug = 'reports';

public function mount(): void
{
$this->data['spend'] = $this->getFilteredSpend();
}
class Report extends Page implements HasForms, HasInfolists
{
use InteractsWithForms, InteractsWithInfolists, HasFiltersForm, WithPagination;
use InteractsWithPageFilters;

protected $data = [];
public int | string $perPage = 2;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.report';

protected static ?string $title = 'Reports';

protected static ?string $slug = 'reports';

public function mount(): void
{
$this->data['spend'] = $this->getFilteredSpend();
}
public function filtersForm(Form $form): Form
{
return $form->schema([
Forms\Components\Grid::make(4)
->schema([
Forms\Components\Select::make('board')
->label('Board')
->placeholder('Select a board')
->options($this->getBoardOptions())
->default(''),

Forms\Components\Select::make('client')
->label('Client')
->placeholder('Select a client')
->options($this->getClientOptions())
->default(''),

Forms\Components\Select::make('organisation')
->label('Organisation')
->placeholder('Select an organisation')
->options($this->getOrganisationOptions())
->default(''),

Forms\Components\Select::make('task')
->label('Task')
->placeholder('Select a task')
->options($this->getTaskOptions())
->default(''),
]),
]);
}
public function filtersForm(Form $form): Form
{
return $form->schema([
Forms\Components\Grid::make(4)
->schema([
Forms\Components\Select::make('board')
->label('Board')
->placeholder('Select a board')
->options($this->getBoardOptions())
->default(''),

Forms\Components\Select::make('client')
->label('Client')
->placeholder('Select a client')
->options($this->getClientOptions())
->default(''),

Forms\Components\Select::make('organisation')
->label('Organisation')
->placeholder('Select an organisation')
->options($this->getOrganisationOptions())
->default(''),

Forms\Components\Select::make('task')
->label('Task')
->placeholder('Select a task')
->options($this->getTaskOptions())
->default(''),
]),
]);
}
/**
* Our infolist record
*
* @param Infolist $infolist
* @return Infolist
*/
public function productInfolist(Infolist $infolist): Infolist
{
/* Get filtered spend records */
$spend = $this->getFilteredSpend();

/* We need to redefine this for pagination */
$this->data['spend'] = $spend;

/* Map the data, including organisations due to many to many relation */
$arr = $spend->map(function ($item) {
/* Transform organisations into a comma-separated string */
$organisations = collect($item->task->board->project->client->organisations);
$organisationNames = $organisations->pluck('name')->implode(', ');

return [
'id' => $item->id, // Spend ID
'time_in_minutes' => $item->time_in_minutes, // Time spend

/* Task */
'task_id' => $item->task->getKey(), // Task Name
'task_name' => $item->task->name,
'task_desc' => $item->task->name,
'task_estimation' => $item->task->estimation,

/* Project */
'project_id' => $item->task->board->project->getKey(), // Project ID
'project_name' => $item->task->board->project->name, // Project Name

/* Budget */
'budget_id' => $item->budget->id, // Budget ID
'budget_name' => $item->budget->name, // Budget Name

/* Board */
'board_id' => $item->task->board->getKey(),
'board' => $item->task->board->name,

'organisations' => $organisationNames,
'client' => $item->task->board->project->client->name,
'owner' => $item->user->email,
];
})->toArray();
/**
* Our infolist record
*
* @param Infolist $infolist
* @return Infolist
*/
public function productInfolist(Infolist $infolist): Infolist
{
/* Get filtered spend records */
$spend = $this->getFilteredSpend();

/* We need to redefine this for pagination */
$this->data['spend'] = $spend;

/* Map the data, including organisations due to many to many relation */
$arr = $spend->map(function ($item) {
/* Transform organisations into a comma-separated string */
$organisations = collect($item->task->board->project->client->organisations);
$organisationNames = $organisations->pluck('name')->implode(', ');

return [
'id' => $item->id, // Spend ID
'time_in_minutes' => $item->time_in_minutes, // Time spend

/* Task */
'task_id' => $item->task->getKey(), // Task Name
'task_name' => $item->task->name,
'task_desc' => $item->task->name,
'task_estimation' => $item->task->estimation,

/* Project */
'project_id' => $item->task->board->project->getKey(), // Project ID
'project_name' => $item->task->board->project->name, // Project Name

/* Budget */
'budget_id' => $item->budget->id, // Budget ID
'budget_name' => $item->budget->name, // Budget Name

/* Board */
'board_id' => $item->task->board->getKey(),
'board' => $item->task->board->name,

'organisations' => $organisationNames,
'client' => $item->task->board->project->client->name,
'owner' => $item->user->email,
];
})->toArray();
This goes with above...
return $infolist
->state(['spend' => $arr])
->schema([
RepeatableEntry::make('spend')
->label('')
->schema([

]),
]);
}
return $infolist
->state(['spend' => $arr])
->schema([
RepeatableEntry::make('spend')
->label('')
->schema([

]),
]);
}
/**
* Handle form submission and update the data.
*/
public function formSubmit()
{
$this->data['spend'] = $this->getFilteredSpend();
}

/**
* Get the filtered data based on request parameters
*
* @return LengthAwarePaginator
*/
protected function getFilteredSpend(): LengthAwarePaginator
{
if (!empty($this->filters)) {
$filters = $this->filters;
} else {
$filters = [];
}

$spend = SpendService::meilisearch($filters);

return $spend->paginate($this->perPage);
}
/**
* Handle form submission and update the data.
*/
public function formSubmit()
{
$this->data['spend'] = $this->getFilteredSpend();
}

/**
* Get the filtered data based on request parameters
*
* @return LengthAwarePaginator
*/
protected function getFilteredSpend(): LengthAwarePaginator
{
if (!empty($this->filters)) {
$filters = $this->filters;
} else {
$filters = [];
}

$spend = SpendService::meilisearch($filters);

return $spend->paginate($this->perPage);
}
So if I choose a filter, then take it off, I have to hard refresh to get the data back. Rather than a live update Ohh, it turns out that if removed, $this->filters still has the keys as empty strings rather than going back to null
Want results from more Discord servers?
Add your server