Vaadin 8 retreving row index in grid

Hello,

In Vaadin 8, The grid functions “edit row” and “scroll to” require the item index. We want to use the functions in a component renderer which contains buttons.

We don’t know how to find the index of the item we want to edit or scrollTo when using a Lazy BackEndDataProvider linked to a DBMS.

Thanks for helping.

Hi,

There isn’t any automatic way to map indices to items and the other way around. For ListDataProvider, it can be easily done based on the backing list. For BackEndDataProvider it might not be so easy.

One solution is to store the the index for each item in the item itself as a (transient) property.

-Pontus

Hi, thanks for your response.

We have already tried that. But it’s not easy especially when sorting columns.

I’m using a CallbackDataProvider, using rank() over (…) allows me to add an reliable index property to my objects, even if sorting and filtering is used.
still, if I create an new object, save it (insert into …) select it grid.select(Object),
I can’t know it’s index thus can’t call scrollTo(idx).
no click event is triggered on select(), only the select event that has no getRowIndex() method is,
the selected object (getSelectionModel().getFirstSelectedItem() …) returns the new object which doesn’t know about rank,
not the one retrieved by the CallbackDataProvider … painful.

With ListDataProvider this is ofcourse trivial, but with large data sets it is a problem

I have put some assorted ideas here

https://github.com/vaadin/framework/issues/10935

and here

https://github.com/vaadin/framework/issues/10115

thanks, I saw these 2 but they solve the opposite problem, here we have the object, not the index of the row.

I’ve solved this by capturing the Query object in the lambdas of the CallbackDataProvider constructor,
I can then do use this to make a DB query to retrieve the index of the object, then I can call scrollTo(idx)

int rank = sqlGetRank(item); -> use saved Query object to parametrize the SQL query that uses rank() over (…)
deselectAll();
getDataProvider().refreshAll();
select(object);
if (rank >= 0) scrollTo(rank);

later I thought that if I add a new Item into the DB I should reset the filters before scrolling to it or I might not see it,
I then don’t need to capture the Query object, I simply clear the filters, sqlGetRank(), deselectAll(), refreshAll(), select() and then scrollTo()

I see, there is yet another discussion about that here:

https://github.com/vaadin/framework/issues/9266