Pb method scrolltoindex in "beforeEnter" method

Hi EveryOne.
I’ve a problem with grid method scrolltoindex().
I want to scroll my grid to the last position after comeback from another screen.
I use this function in the “beforeEnter” method in my view but it doesn’t work, my grid begin always at the first row. In this place, I haven’t problem with the “select” method of my grid but not with “scrolltoindex()”. Have you an idea ? Thank you in advance

Hi, there seems to be an issue with the order in which the js commands are executed on the client in this case: scrollToIndex is called on the grid web component before it’s populated with data.

Can you file an issue for this at Issues · vaadin/flow-components · GitHub

As a temporary workaround, you can schedule an additional delayed call to scrollToIndex with:

// Additional call to workaround the issue
grid.getElement().executeJs("setTimeout(() => this.scrollToIndex(20))");

I strongly suggest that it should be called in afterNavigation instead of beforeEnter. Before enter is meant to perform things that are done before view navigation cycle is complete.

Thank you for your help. I’ve trying to use scrooltoindex in “afternavigation”, and I have the problem. With Tomi workaround solution it’s OK. So I follow your suggest Tatu and use the workaraund in “afternavigation”. I’m going to fill the issu in GitHub. Thank you I’m not blocked now :slight_smile:

I have another question. After call “scrolltoindex()” , I want to select the line that was selected before. I call the method "grid.select(item) but the line is doesn’t selected. Perhaps I can use the same workaround but I have a Js bug with this code : myGrid.getElement().executeJs(“setTimeout(() => this.select(item))”). Have you an idea ?

Hi, grid.select(item) should work fine without workarounds because it doesn’t rely on timings on the client side.

Does grid.select(item) work if you run it in your app without navigation involved?

Yes it work fine but thanks to you I’ve found the solution. In some particular case, my item was null. That’s good now.