Questions about migrating from Vaadin7 Table to V8-Grid

I’m migrating from Vaadin7 Tables to Vaadin8 Grids using a ListValueProvider and I have a few question how to do it. Until now, I was using a BeanItemContainer as the Datasource of my Table. To update an item in the datasource I used to determine the index of my item, delete the item by index and put the updated item in the datasource at the same index. Using the Vaadin8 Grid, I tried to use the refreshItem Method of the ListDataProvider. This seems to work but when I call refreshAll(), all my updates are lost. Is this a Bug or do I have to use another way of updating items? I tried to do it the same way as before (figure out which item must be updated, delete the old one and put in the new one) but without a way to determine the index, I have to iterate over all items to find the old one.

Another question is how I can select the first row in an already sorted Grid? I tried both, sorting the Grid and sorting the DataProvider. Both approaches do not sort the underlying Collection, so how can I figure out which item is located in the first row of the grid? With the Vaadin7 Table I could use firstItemID(). Is there a similar method in vaadin8?

The last question is about the scrollTo method. It’s first parameter is the rowID, but how can I get this information if I want to jump to a specific item in an already sorted grid? That too was easy with Vaadin7 Table using the setCurrentPageFirstItemId method.

Thanks in advance for any ideas!
–Björn

Hi Björn,

When you make a
ListDataProvider
from a
Collection
, the collection is used as the “backend” of your data provider. If you make some changes to the items in the
ListDataProvider
(the items themselves, not copies of it) the
DataProvider.refresh
should do everything you need.

In the case of replacing items in the list with new items, you should find the old one in the collection ( assuming it’s a list:
myList.indexOf(oldItem)
) and replace it (
myList.set(index, newItem)
). After replacing you need to tell the
Grid
you changed something with
DataProvider.refreshAll
.

The sorting information is accessible through the data communicator of the
Grid
and picking up the first item in the sorted list can be done like this:

list.stream().sorted(grid.getDataCommunicator().getInMemorySorting()) .findFirst().ifPresent(grid::select); This will get the current sorting instructions for in-memory data from the
Grid
and use it to sort a stream from your backing collection and pick its first value and select it.

The
scrollTo(item)
is a thing we’ve been thinking of how to solve in an efficient way since, as you too said, most of the time you’d end up going through all the items until you find what you need, and that is not something we’d want to do for big data sets in slow DB queries. There’s a replacement called
scrollTo(int rowIndex)
and you can use the stream above to sort it, and instead of
.findFirst…
use
.collect(Collectors.toList).indexOf(item)
to find the index of the specific item.

//Teemu