How to workaround out of sync values retrieved in the selection listener of a vaadin’s grid

If you compose a sophisticated application with vaadin, you may face an interesting issue: values in the grid got updated (say, via HTTP) and you can see them in the corresponding rows and columns, but you can’t read the actual values when selection listener gets triggered. I will tell how to workaround this issue.

I tried to reproduce this behaviour and created a simple vaadin view with a grid and a couple of buttons. But obviously I missed some difference from my application (tabs or the way the rows get updated, for example), and I failed. Even values with a straightforward type boolean got retrieved incorrectly (as opposed to some nested entitites). Nevertheless I needed to read actual values from the grid (I could read them only after re-login). I can use an analogy: if you drive a car, you want it to turn right every time you rotate the steering wheel clockwise. In our case the car can turn left, if you had rotated the wheel counterclockwise previously (the direction got cached somehow). May be the car turns right only if there number of passengers is even.

First of all I inspected the relevant forums. Some threads described similar issues like this one: issue and advices but it didn’t help.

So, I made it my way. I have a class Container with annotations @SpringComponent and @UIScope, so it stores data specific to each user. In this container I store a list List of the objects. On every refresh of data represented in the grid I not only update the grid removing all the items via

boxesGrid.getContainerDataSource().removeAllItems()

and adding the new entities via

newList.forEach(ent -> entGrid.getContainerDataSource().addItem(ent))

, but also refresh the list in the container (this covers cases, when entities get added/removed/changed compared with the previous state of the list). In the selection listener I read the selected entity, but I use only its id and search for the entity in the container’s list using this id. The grid may cache whatever it wants, I don’t care any longer.

You can leave a response, or trackback from your own site.

Leave a Reply