Empty initial state on list resource page?

Hi! Is it possible to start with an empty initial state on the list resource page? We have some very large resources where showing the first x entries is redundant. We only want to start showing results once a filter or search is used. Thanks in advance!
16 Replies
Dennis Koch
Dennis Koch2y ago
You can overwrite the query and add a false query so it returns 0 results until you filter. You can get the filters via fn($livewire) => $livewire->activeFilters
TiBiBa
TiBiBaOP2y ago
I found a way to by default return 0 entries. But how do I remove this getTableQuery() change once we start filtering? Can't figure this out. Thanks in advance! I just found that this question has been asked previously: https://discord.com/channels/883083792112300104/1082256110402162688 However, the provided solution doesn't work as $this->activeFilters is not found. Should I declare this somewhere?
toeknee
toeknee2y ago
Are you doing this on the resource or the list page since it wouldn't be on the resource
TiBiBa
TiBiBaOP2y ago
On the list page I just created a work-around to get it working:
private function countActiveFilters(): void
{
$this->filterCount = 0;
foreach ($this->getTableFilters() as $filter) {
$data = array_values($this->getTableFilterState($filter->getName()));
$this->filterCount += count(array_filter($data));
}
}
private function countActiveFilters(): void
{
$this->filterCount = 0;
foreach ($this->getTableFilters() as $filter) {
$data = array_values($this->getTableFilterState($filter->getName()));
$this->filterCount += count(array_filter($data));
}
}
toeknee
toeknee2y ago
Is $this->table->activeFilters not set? I had a feeling it was but could be wrong
TiBiBa
TiBiBaOP2y ago
This returns an Undefined property: Filament\Tables\Table::$activeFilters I was hoping that the active filters would be stored somewhere as the work-around is a bit cumbersome and inefficient. For so far no luck finding it
Dennis Koch
Dennis Koch2y ago
Can you share your code? Not sure why it is Filament\Tables\Table as it should be on the Page
TiBiBa
TiBiBaOP2y ago
It is a class that extends the ListRecords class, now building the query like this:
private int $filterCount = 0;

protected function getTableQuery(): Builder
{
$this->countActiveFilters();
return parent::getTableQuery()
->when(
$this->filterCount == 0,
fn ($query) => $query->where(DB::raw('1 = 0')),
);
}
private int $filterCount = 0;

protected function getTableQuery(): Builder
{
$this->countActiveFilters();
return parent::getTableQuery()
->when(
$this->filterCount == 0,
fn ($query) => $query->where(DB::raw('1 = 0')),
);
}
Dennis Koch
Dennis Koch2y ago
And how did your code look like, that produced that error?
TiBiBa
TiBiBaOP2y ago
I only try some debug coding, using a dd() using the following line as suggested by @toeknee_iom.
dd($this->table-activeFilters);
dd($this->table-activeFilters);
Dennis Koch
Dennis Koch2y ago
What was the issue with $this->activeFilters as in my original suggestion and the one in the post you linked!?
TiBiBa
TiBiBaOP2y ago
Also resulted in an error: Property [$activeFilters] not found on component When using something like: dd($this->activeFilters);
Kenneth Sese
Kenneth Sese2y ago
The tables active filters are stored in tableFilters not activeFilters
Dennis Koch
Dennis Koch2y ago
Yeah, just figured that out, too. I guess I was misleaded by $activeTab
Kenneth Sese
Kenneth Sese2y ago
Make sure this doesn’t give you a false negative when a ternaryFilter is active but false
TiBiBa
TiBiBaOP2y ago
Thank you very much for the help!
Want results from more Discord servers?
Add your server