Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
All based on latest Vaadin Version 7.5
How to get the current Scroll position from a Grid?
I can set the scroll position based on an ItemId, but i did not find anything to get the position or the current row index shown.
That information is not easily available in Grid due to lazy loading and caching of rows. What is the use-case you are trying to solve with this information?
I like to store the current scroll position for usability for the user.
even if the component itself, the container or the Grid is detached.
After recreating the Grid i like to position the Grid to the last known position.
It would be helpfull to get the index of the upper row or the itemId itself of the current container position in the Grid. But neather is possible as i see it.
This is actually a bug somewhere in the Grid, since this functionality is implemented in the Escalator, which Grid is based on. For some unknown reason, the Grid loses it's scroll position even though it should not. It has been brought to our attention before at https://vaadin.com/forum#!/thread/10203518 .
You can follow the ticket to be informed on changes. Don't hold your breath while waiting, since as of now the Framework development team is rather overburdened with many other issues. Making the ticket a bugfix priority will most likely force it to our attention faster.
Ok, but that is not what i intent to do.
- Get current Row index (or ItemId) that the user currently scrolled to (to upper most visible Item).
- Store this Item somewhere (Maybe Databse or Session or ....) -> its not quite whatwe do, but its easier to explain.
- remove the grid from the View or remove the View. Point is the Container and the Grid are gone.
- Recreate the View with Grid and new Container.
- Here it comes: position the Grid to the last known possition for the User (readed from Session/DB or elsewhere).
independent from the Ticket you posted, the Grid never could hold the position, cause i removed it. For this Case its Usefull to have a getter for the Row Index or ItemId Visible at current Scroll Position. I can Set the position, but never read it.
In theory the client-side of the Grid should know what is the top most row being shown to the user, and this information could be sent to the server-side. How ever there are some complications related to lazy-loading and sending of the information. You can try to fix this by doing an Extension for the Grid, listening to scroll events and monitoring the row key of the topmost row. This information can be then sent to the server via Delayed RPC.
Some issues come up since doing this for every time the top row changes will send a huge pile of RPC requests to the server. You can use a timer, that waits for a bit before actually checking the top row, and reset the timeout everytime a new scroll event comes. Once the information is sent to the server, it needs to be stored in a static way, so the new Grid with the new Extension will know the previous one. Container sorts and filters should also be taken into account when making this kind of "persistent Grid state".