Crash on getTableFilterState() on select filter with relationship
Hi! We are looking to prevent initial loading of the first x resources when opening a list resource page and came up with a solution like this:
Whereas the
countActiveFilters()
is as follows. This works fine when using a text input filter or a select filter with manual options. But when using a select filter based on a relationship the page crashes. Is there any way to improve this code? Thanks in advance!
10 Replies
I attempted to improve this code by using the
$this->tableFilters
value but this is always the amount of filters, not the amount of active ones. So this didn't work:
You could try something like this:
Thanks for your reply! As I use custom filter forms the data is nested and will always return a count of the amount of attributes:
It seems the issue is the combination of custom form filters and a default selectFilter based on a relationship. The page crashed on trying to read the values. Might be because one is a nested value from the custom form and the other one is default? Still not sure on the exact cause (and solution...)
Did you try to create a Select field in the form filter?
No the select if a default selectFilter, as described in the documentation. Like this:
The textual filters are using a custom form like this:
Ok. I think you can keep selectFilter. What is the error on the browser?
Tha'ts the weird thing. The browser doesn't throw an error. It just keeps loading and returns a white page after a while
Should work...
Can you share the project on github?
Checking for a filter being active is complicated because of all the different ways filters can be active and, as I had mentioned in your previous help, can give false negatives depending on the filter type. You can try to check each filter against its default value to see if it’s active or not. The most reliable method I have found, which is weird, is to check against the indicator status. Filament does all the heavy lifting for us already to check against default values to display indicators so use that to determine if a filter is active or not. I use this in a slightly different context, but you can adapt to return a count() instead):
This might not work if you have a filter turned on by default (ie, you see a filter indicator when you load the page), but if I’m understanding your question correctly, no filters are turned on by default so this should work.
BTW, this is how filament determines whether or not the little filter indicator dot should be displayed.
Unfortunately the code snippet crashes on the
getIndicators()
function when being called on the select filter with relationship. Resulting in the same white page issue as before