Howto get sorted visible/rendered grid items only

I use a grid with MultiSelect.
The grid is dynamically filled with database content via a backendDataProvider.
I would like to achieve the following:
I want to show all on the grid selected rows in a new dialogue, but in exactly the same order that is currently used by the grid.
My grid has several columns, all of which can be sorted. Therefore, the selected rows can be visible in different orders.

A naive approach would be to remember the SortOrder from the backend fetch method. If the dialog is created, this sortOrder can be used to create a Comparator object via switch case block (for all poosible columns/sortOrders) or similar, which is then applied to the set of selected items.

But I have many columns (20+) and such a if/else, switch/case block would be huge.

The method

this.grid.getSelectedItems()

only returns a set in which the sort order is not defined and is therefore out of the question.
It would be much better if there was an methode that only return the currently visible or already fetched rows in exactly the same order in which they are currently visible/rendered by the grid.

In DataProvider, DataCommunicator or GridDataView there does not seem to be such a method.

How can I restore the sort order for the selected items ?

Grid::addSortListener

And some cumbersome steps to re-create / store what the user did…

There was also a ticket in GitHub about investigating improvements on how to get the current visible/filtered items but I can’t find it.

Why don’t you store the parameters that you pass to the repository in the data provider?

This seems to be the same problem:

Export the data currently shown in a grid, taking into account filtering and sorting

The probles is less the sort/filter params, that are passed to the repo. As I mention, they can be restored from the parameter of the fetch method used by the ConfigurableFilterDataProvider.

The problem are the grid items resulting from that query. After the are overtaking to the grid, there seems to be no possibility to gain access to these items only. Especially the call of dataProvider.getItems() for such a lazy/backend scenario first call the fetch method multiple times until ALL items are return from the database and then returns those huge list, which is not, what I expect at this moment.

But you have a point: we could cache the items we get from the backend. It’s a little bit dirty to burn the memomry but hey … the garbage collector to the rescue … :wink:

There was also a discussion about the meaning of selected items for continuous scrolling vs. paginated Grid:

I wouldn’t even cache them.

Just call the repository again when opening the dialog

But assume the grid always has called the fetch method 3x, for every 50 items 1-50, 51-100, 101-150. Now, 150 items are visible in the grid. From those 150 the user selects some, lets say the 1st
2nd,
75th
125th

item. If we cache the previously fetched items, we have 150 in our cache.
If we simple re-call the fetch method with the last used parameters, only the last 101-150 items are loaded from the database.

Grid doesn’t know the ordering of items that are no longer in the active view. The order returned by getSelectedItem() is actually defined even though the documentation seems lacking - it’s ordered by the order in which the items were selected.

I would also suggest doing a similar query again. You can cache the last sort order passed to the data provider and then you do a new DB query with the same sort order but with a little bit of additional SQL along the lines of where id in(<list of selected item ids>). In that way, you only need to re-fetch the items that are actually selected.

yep, that

additional SQL along the lines of where id in(<list of selected item ids>)

cames in my mind too, as I thought about Simons suggestion…

1 Like

That’s what I wanted to suggest but I forget to mention die IDS ;-)

1 Like