Query only toggled/displayed columns

I have a table with 100+ columns and for performance reasons the table displays only limited amount of them using toggleable( . However, all columns are selected from DB. First I thought that it's a bug, why wouldn't Filament select only the columns that are defined on table but then realised that developer can set state using full record. So I'm looking for a way to manually set the query with select( but for that I need to access toggled columns. Is there a way to do that?
Solution:
The query need to be set as closure ```php $table->query(function () { // obtain $query // get $selectableColumns from script above...
Jump to solution
6 Replies
ConnorHowell
ConnorHowell8mo ago
I don't think on the table there's a method to get all toggled columns, but you can call getColumns on the table, then loop through each column and call isToggledHidden on the column to see if it's toggled
KarlisJ
KarlisJ8mo ago
hm, there's no method to check if it's toggled. And even so, I need to build the query around the same time that table is defined. I found this code in the source
if (! count($this->toggledTableColumns ?? [])) {
$this->getTableColumnToggleForm()->fill(session()->get(
$this->getTableColumnToggleFormStateSessionKey(),
$this->getDefaultTableColumnToggleState()
));
}
if (! count($this->toggledTableColumns ?? [])) {
$this->getTableColumnToggleForm()->fill(session()->get(
$this->getTableColumnToggleFormStateSessionKey(),
$this->getDefaultTableColumnToggleState()
));
}
dd(session()->get($this->getTableColumnToggleFormStateSessionKey()));
dd(session()->get($this->getTableColumnToggleFormStateSessionKey()));
Gives me full list of all columns and true/false.
ConnorHowell
ConnorHowell8mo ago
I'm trying to figure out the best way to achieve it because I'm curious now lol, getVisibleColumns is available on the table which should give you a list of columns that aren't hidden and are currently visible
KarlisJ
KarlisJ8mo ago
Yeah, that doesn't work well, because I need to define the query in the same time that columns are defined the method returns empty array Tried setting columns to the table and then calling the getVisibleColumns but getting all columns. I think I can get it working with $this->getTableColumnToggleFormStateSessionKey() but the value is empty on first load. Sadly, getDefaultTableColumnToggleState can't be used for fallback. Getting
Typed property App\Filament\Pages\ListRegistryEntries::$table must not be accessed before initialization
$selectableColumns = collect(session()->get($this->getTableColumnToggleFormStateSessionKey()) ?? [])
->filter(fn(bool $value, string $column) => $value )
->keys()
->toArray();
$selectableColumns = collect(session()->get($this->getTableColumnToggleFormStateSessionKey()) ?? [])
->filter(fn(bool $value, string $column) => $value )
->keys()
->toArray();
Coupled with
$query->select($selectableColumns)->addSelect('id');
$query->select($selectableColumns)->addSelect('id');
works. But only once user has interacted with togglable form. It's enough for me, since I'm in control of which columns are toggled by default and can use that as the fallback
Solution
KarlisJ
KarlisJ8mo ago
The query need to be set as closure
$table->query(function () {
// obtain $query
// get $selectableColumns from script above
return $query->select($selectableColumns)->addSelect('id');
})
$table->query(function () {
// obtain $query
// get $selectableColumns from script above
return $query->select($selectableColumns)->addSelect('id');
})
because session is updated only once the table is initialized. So when user checks new column to toggle, it's not yet in the session. Using closure fixes that.
KarlisJ
KarlisJ8mo ago
This was fun, thanks @ConnorHowell for joining