platform icon indicating copy to clipboard operation
platform copied to clipboard

Filter and Sort not working

Open BrianWendt opened this issue 3 years ago • 7 comments

Describe the bug I can't filter or sort on any lists. Out of the box Users list can't even be filtered or sorted which I'd expect would work since it's the defaults. I can't filter or sort on other tables, but at the least I figured users and roles would work.

To Reproduce Steps to reproduce the behavior:

  1. Install clean copy of Laravel ^9.19 (9.29)
  2. Install Orchid 13.1 per instructions on site
  3. Add a few test users
  4. Try filtering by name or email address

Expected behavior I expect that sorting and filtering will work.

Screenshots orchid-bug

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome 105.0.5195.102
  • Version [e.g. 22]

Server (please complete the following information):

  • Platfrom Version: Windows 10 running XAMMP 3.3
  • Laravel Version: 9.29
  • PHP Version: 8.1.2
  • Database: MariaDB
  • Database Version: 10.4.22-MariaDB - mariadb.org binary distribution

Additional context Add any other context about the problem here.

BrianWendt avatar Sep 10 '22 07:09 BrianWendt

Remove UsersFilter::class in queue of UserListScreen

Cregennan avatar Sep 10 '22 09:09 Cregennan

@Cregennan , thanks for the reply.

I commented out ->filters(UserFiltersLayout::class) in the query() method and UserFiltersLayout::class in the layout() method.

That did not fix the problem.

BrianWendt avatar Sep 10 '22 19:09 BrianWendt

I'm doing some experiments to try finding the issue.

I've confirmed that the sort is being added to the query in orchid\platform\src\Filters\HttpFilter.php:203

BrianWendt avatar Sep 10 '22 19:09 BrianWendt

@Cregennan

Oh, I get what you were saying. Change ->filters(UserFiltersLayout::class) to ->filters()

That fixes the filtering and sorting to work with name and email. However, I can't filter on role anymore. It's one or the other?

I also found that I had been calling defaultSort before the filters method on the other models. When I make sure to apply filters() first, I was able to get the other list screens to work. The order of things being called doesn't seem to be documented but the example found in "Automatic HTTP Filtering and Sorting" does show the order correctly. My bad.

BrianWendt avatar Sep 10 '22 19:09 BrianWendt

Order that these are called in is very important it seems. The below code is working.

return [
            'users' => User::with('roles')
                ->filters(UserFiltersLayout::class)
                ->filters()
                ->defaultSort('name', 'asc')
                ->paginate(),
        ];

Call filters() empty after the other is what got it working.

I'd suggest either making it so that the order that these methods are called does not impact the query (besides paginate for obvious reason). Otherwise, document that the order is important.

BrianWendt avatar Sep 10 '22 20:09 BrianWendt

@BrianWendt With the release of the update, this should be correct, please check.

tabuna avatar Sep 11 '22 01:09 tabuna

@BrianWendt With the release of the update, this should be correct, please check.

For me not work. Same problem after update today to last release and using @BrianWendt suggestion. In my case order of filter or change ->filtersApplySelection(UserFiltersLayout::class) to ->filters(UserFiltersLayout::class) don't solve the problem.

gioacchinopoletto avatar Sep 11 '22 11:09 gioacchinopoletto