Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$livewire->getTableFilterState() and $livewire->getTable()->getFilters() do not return currently selected value #15726

Closed
epertinez opened this issue Feb 26, 2025 · 2 comments

Comments

@epertinez
Copy link

epertinez commented Feb 26, 2025

Package

filament/filament

Package Version

v3.3.0

Laravel Version

v10

Livewire Version

v3

PHP Version

8.3

Problem description

I understand that nobody is going to look at this problem because I don't have time or knowledge to create a easy repository with an example, but I think it is an important problem so, here you have it, just in case.

I have a resource with products that are related to a category (how not). Categories are hierarchical and can have many levels, but filtering by 3 levels is enough for me.

So I create the filters of the table so the first shows the first level categories, and the next two get filled in depending on the previous filter:

public static function table(Table $table): Table
    {
        return $table
 ->filters([
                SelectFilter::make('categoria_principal')
                    ->options(fn () => Category::where('id_parent',2)->get()->pluck('name', 'id_category')->toArray())
                    ->query(fn ($query, array $data) => $query->inCategoryTree($data['value']??'1'))
                    ,
                 SelectFilter::make('categoria_secundaria')
                    ->options(function ($livewire) { 
                        $categoriaPrincipalId = $livewire->getTableFilterState('categoria_principal')['value']??null
                        if (empty($categoriaPrincipalId)) return [];
                        return Category::where('id_parent', (int)$categoriaPrincipalId)
                            ->pluck('name', 'id_category')
                            ->toArray();
                    })
                     ->query(fn ($query, $livewire) => $query->inCategoryTree($livewire->getTableFilterState('categoria_secundaria')['value']))
                ,
                ])
...

Now, that works at the first time, but once I change the value of the first filter, even when options of the second is recalculated, $livewire->getTableFilterState() and $livewire->getTable()->getFilters() do not return the new selected value but the previous one (the one on the url).

Don't think that is how it should work, but appart of that, I found no way to return the actual current value of the new selected option.

Neither I found a way to force refresh the page on select change, which is not ideal but would solve the problem.

Expected behavior

$livewire->getTableFilterState() and $livewire->getTable()->getFilters() should return the new values or at least there should have to be a way (documented?) to know what is the real value of the selected option, so you can act on those to decide on other filter behaviours.

Steps to reproduce

Create a Resource attached to a table
Create filters as shown.
See that while debugging or /dd($livewire->getTableFilterState('categoria_principal'),$livewire->getTable()->getFilters()['categoria_principal']->getState());/ inside of the options function of the second selectfilter, you get the first value of the filter and not the new one.

Reproduction repository (issue will be closed if this is not valid)

.

Relevant log output

Copy link

Hey @epertinez! We're sorry to hear that you've hit this issue. 💛

However, it looks like you forgot to fill in the reproduction repository URL. Can you edit your original post and then we'll look at your issue?

We need a public GitHub repository which contains a Laravel app with the minimal amount of Filament code to reproduce the problem. Please do not link to your actual project, what we need instead is a minimal reproduction in a fresh project without any unnecessary code. This means it doesn't matter if your real project is private / confidential, since we want a link to a separate, isolated reproduction. That would allow us to download it and review your bug much easier, so it can be fixed quicker. Please make sure to include a database seeder with everything we need to set the app up quickly.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2025
@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Feb 26, 2025
@drithh
Copy link

drithh commented Mar 2, 2025

have u got any solution on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

2 participants