Vaadin 8 Grid.scrollTo(x) but, where is x?

A singletone Grid might be a good trick, haven’t tested that though but theoratically it should work as well :slight_smile: In General Vaadin stores the state on the server side inside the object, so that might sound like a lighter solution!


A^2

I also thought about keeping the view instance but I don’t know if it’s possible (I can navigate from one view instance to another view instance so I don’t want to keep too much instance of this view).
I will check if the solution can be used (easier to do that way :slight_smile: ).

For grid or Vaadin objects, I like to do this way (found in some examples of code :wink: )

public Grid<Person> getGrid() {
  if (grid == null) {
    grid = new Grid<Person>();...
  }
  return grid;
}

So I don’t need to create a grid on constructor ou PostConstruct (for spring view) not on enter.

I believe that my situation is quite similar to yours.

On my application I’m trying to use a
Wizard
component with a Grid in the step one, but when the user is on step two and tries to get back the previous step he losts the previous state. What I did was to maintain the previous loaded grid data in a list in memory (cache) + selected the editing item with grid.select(item) option. But I can’t scroll to the selected one.

If someone here has a better solution to do this please tell us here!

For this problem, I used grid-scroll-extension, save the scroll position and go to the saved position.
It’s working if the data is in memory or with a lazy data provider.
but of your data changed then the scroll position will be wrong.

The thread is here:
https://vaadin.com/forum/#!/thread/16886923

Perhaps the best solution for a wizard is to keep the grid (so you won’t lose the scroll position of the grid).

The workaround given in the github thread is working if you don’t sort the grid.

This not solve problem, which appear when added new item.
It’s was very bad idea to make Vaadin grid with “virtual scrolling”. With old good “paging” not so many problems.

Luckily this has been changed. At least the Vaadin 8.5.0.rc1 the Grid Javadoc for Grid.scrollTo(int) reads:

     * @param row zero based index of the item to scroll to in the current view.

So it appears that you can simply pass in 0 for the first row, 1 for the second row, etc.

So, I have a sorted Grid of significant size and want to add an item, select the item, and then scroll to the items position within the sorted grid (ie x). Without implementing a bunch of listeners and maintaining a (hopefully) synchronized version of what is show in the UI, How do I find what row (int x) the new record is in so that I can call scrollTo(x)?

Back to the original question.

From Vaadin 8.5+ there is getRowIndex() in Grid’s item click event. That brings some symmetry to the API. I think it is the most common use case, i.e. you click an item and you want to do something with it.

I have the same issue with a grid (Vaadin 8.5.2):
I need to keep selected rows selected and the selection visible after applying a value filter.
I manage to restore the selection.
For the scroll of the grid I tried the suggested workaround with the ArrayList conversion of getItems() of my Dataprovider.
The suggested conversion (ListDataProvider) grid.getDataProvider()).getItems() does not work for me, as getDataProvider().getItems() is not defined.
When using my DataProvider variable instead (defined as ListDataProvider), the conversion works, but the indexes I find are of course outside the bounds of filtered List and cause Exception.
So my only solution will be to provide a pre-filtered List somewhere and change in DataProvider, which really seems a workaround outside the intentions of DataProvider to me.
I would really appreciate a filter option preserving the selection and a method scrollTo(instance of Bean) instead of scrollTo(index).

The problem ListDataProvider#getItems() is, that it returns the backend list without filtering and sorting criteria applied.
To take sorting and filtering into account the following implementation of scrollTo(bean) should work with ListDataProvider:

public void scrollTo(T item) {
    DataCommunicator<T> dc = grid.getDataCommunicator();
    List<T> items = dc.fetchItemsWithRange(0, dc.getDataProviderSize());
    grid.scrollTo(items.indexOf(item));
}

Other implementations may also be possible by extending DataCommunicator (necessary because some methods needed are protected).

Thank you so much, Harry.
I could implement it the way you recommended.

Nice to here it works for you - how about a thumbs up then :slight_smile: