Grid and preserving selection

Hello. I have a problem with Grid. It’s about lazyDataProvider and maintaining selection data. For each row, I also have an index from the backend, which I want to use with the ScrollToIndex method. The problem is that after sorting, when the selection is preserved, the index does not change.
Index is always populated from backend from 0 to n, after every sort.

To be more specific:

  • I select the first record in the Grid, which has index 0
  • I sort in reverse
  • This record still has index 0, but if I deselect and reselect it, the index updates correctly

Can you show us some code?

Sorry my mistake, my selected bean has not updated index

Hi. I am back on this problem.

Here is a simple code:

public void onCLose(Crud crud, DataRow dataRow) {

    grid.deselectAll();

    grid.getDataProvider().refreshAll();
    grid.select(dataRow);
    grid.scrollToItem(dataRow);

The problem is, that index in my DataRow isn’t updated with the refreshed one.

It may be the same bug as described here. Scrolling to index does not happen before Grid rendering is complete. And doing refreshAll will retrigger Grid render.

The problem is that DataRow contains index, calculated from backend. After refreshing all, records in grid are resorted. My previously selected item is in different position in grid. But re selecting item and getting selection, returns old index, instead of new one.