Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

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

Joseph Craig
4 years ago Apr 28, 2017 5:30pm

I apologize if this has been asked; however, I cant seem to find this question anywhere.  With Vaadin 8 grid, the dataProvider maintains backend data while what is displayed in the UI may be filtered and sorted entirely different from the backend.  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)?

thanks.

Vasiley Borschenko
4 years ago Jun 04, 2017 8:24pm

Just started to think about moving to Vaadin 8, and ran into that puzzle too. In Vaadin 7 Grid's scrollTo method looks like:

public void scrollTo(Object itemId) throws IllegalArgumentException
Scrolls to a certain item, using ScrollDestination.ANY.If the item has visible details, its size will also be taken into account.

Parameters:
itemId - id of item to scroll to.

which is perfect.

Whereas in Vaadin 8 scrollTo is:

public void scrollTo(int row) throws IllegalArgumentException
Scrolls to a certain item, using ScrollDestination.ANY.If the item has visible details, its size will also be taken into account.

Parameters:
row - id of item to scroll to.

where row is "id of item" of type int. What if in my backend data provider item's id is not "int"? What is actually "row" - index within a grid? If it is, then how do I calculate it, as Joseph Craig rightly asks? If it is not, then what is the value of scrollTo(int row)?

Thanks!

 

Patrik N.
4 years ago Jun 06, 2017 11:17am

Hi,
as you can read here https://github.com/vaadin/framework/issues/8820 and with a look in the source code of vaadin, there is no way to find the row keys actually. They are only maintained at client side.

In my projects this is also a huge drawback to have no scrollTo(T item) because if the user selected an entry in a grid with millions of entrys and switches the view forth and back there is no way to restore the view and scroll to the last selected position. In addition i think it is quite problematical that everything in the view is correctly restored except the scoll position... kind of weird for the customers...

Joseph Craig
4 years ago Jun 16, 2017 7:29pm
Jeffrey Lyon
4 years ago Aug 24, 2017 2:54pm

I'd like to add a +1 on this.  Promoting Grid/TreeGrid as a successor to Table/TreeTable is premature without this feature.

Moritz Christian
4 years ago Aug 25, 2017 8:08am

Me too +1
Actually really important I would say

Jean-Christophe Gueriaud
4 years ago Aug 25, 2017 9:34am

I also a +1 on this github issue. That's really important to return on the grid on the same row

A.Mahdy AbdelAziz
4 years ago Aug 25, 2017 3:29pm
Jeffrey Lyon
4 years ago Aug 25, 2017 6:32pm
A.Mahdy AbdelAziz
4 years ago Aug 25, 2017 11:04pm
Jeffrey Lyon
4 years ago Aug 26, 2017 12:46pm
Tatu Lund
4 years ago Aug 27, 2017 1:42pm
A.Mahdy AbdelAziz
4 years ago Aug 27, 2017 5:38pm
Vasiley Borschenko
4 years ago Aug 27, 2017 7:51pm
A.Mahdy AbdelAziz
4 years ago Aug 27, 2017 7:58pm
Vasiley Borschenko
4 years ago Aug 27, 2017 8:56pm
Tatu Lund
4 years ago Aug 28, 2017 8:05am
Jean-Christophe Gueriaud
4 years ago Nov 30, 2017 3:28pm
A.Mahdy AbdelAziz
4 years ago Dec 01, 2017 7:31pm
A.Mahdy AbdelAziz
4 years ago Dec 01, 2017 7:36pm
Jean-Christophe Gueriaud
4 years ago Dec 01, 2017 9:31pm
A.Mahdy AbdelAziz
4 years ago Dec 01, 2017 10:19pm
Felipe Caparelli
4 years ago Jan 08, 2018 3:43pm
Jean-Christophe Gueriaud
4 years ago Jan 08, 2018 4:31pm
Maksim Paz
4 years ago Jan 24, 2018 6:06pm
Martin Vyšný
3 years ago Jul 26, 2018 7:11pm

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.

Tatu Lund
3 years ago Jul 27, 2018 5:50am
Elke Hofmann
3 years ago Nov 25, 2018 4:19pm
Harry Wissiak
3 years ago Nov 26, 2018 1:30pm

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).

Last updated on Nov, 26th 2018
Elke Hofmann
3 years ago Nov 27, 2018 5:40pm
Harry Wissiak
3 years ago Nov 29, 2018 2:30pm