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:Jump to solution
The query need to be set as closure
```php
$table->query(function () {
// obtain $query
// get $selectableColumns from script above...
6 Replies
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
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
Gives me full list of all columns and true/false.
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
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 initializationCoupled with 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
The query need to be set as closure
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.
This was fun, thanks @ConnorHowell for joining