Grid and scrollToIndex()

But iterating might be be still better performance/memory wise with too many items.

why do you think so?

As that list has to be allocated in memory if it’s created from the stream? :thinking: WDYT?

sure but if there are a lot of items a ListDataProvider is a bad choice anyway.

true that

but instead of the loop one could use this stream syntax:

IntStream.range(0, grid.getListDataView().getItemCount())
     .filter(i -> myItem.equals(grid.getListDataView().getItem(i)))
     .findFirst()
     .ifPresent(i -> grid.scrollToIndex(i));

But this will not be faster

at least more elegant :smile:

Indeed :wink:

Thank you for your help

Sorry for digging this up, is it possible to scrollToIndex in grid using CallbackDataProvider ?

There is no build-in way to retrieve the index from a CallbackProvider since the list is not loaded.

You have to do it manually. (One way is to retrieve the firstpage, check if the item is here, then go to the next page,… but that will load all the data)

Ok, I understand. Thank you JC

Check out this scrollToItem implementation. That “forcefully” iterateds from the start. For many cases this is fine, even if using lazy loading of data (~ callbacks). But, if you have truly large datasets and want to scroll somewhere long, it will get slower and cause more DB requests. That should be easy to figure out with profiler and then you can optimize it with a special query if needed.

Thank you Matti,
Our list is extremely long and we decided to skip the feature that use scrolling. But I will bookmark the link, it will be useful someday.

1 Like

Good decision I guess. Even with customized DB query to detect the index, it might be the worst performing query of your app (heavy for DB). Sometimes it is just wisdom to make a small compromize with the UX.

Probably better for you to invest in a good search functionality, so that people don’t need to find the result from a long list of items.