Grid SelectionModel pre select row

Hi,
I’m using Vaadin to build a project at the University.
I use Vaadin 8.5.2 and I tried to pre select some items in a grid.
Code Snipped:

private Grid<User> userGrid;
private MultiSelectionModel<User> selectionModel;
private User currentUser = userService.getUserById(((MainUI)UI.getCurrent()).getCurrentUserId());
List<User> users = userService.getAllUsers();

userGrid = new Grid<>(User.class);
userGrid.setColumns("name", "availability");
userGrid.setItems(users);

selectionModel = (MultiSelectionModel<User>) userGrid.setSelectionMode(SelectionMode.MULTI);
selectionModel.deselectAll();
selectionModel.select(currentUser);

In my oppinion this should work. I also have a selection listener:

userGrid.addSelectionListener(event -> {
	Notification.show(event.getAllSelectedItems().size() + " items selected");
});

And the selection listener said that 1 item is selected but the checkboxes in the UI are all empty.
Please find picture attached.

Do you have any idea on this?

Best and thanks,
Christian

17280946.jpg

Are you using @Push?

No I don’t.
DO I need it? I tested selectAll() and this works.

We annotated @Push(PushMode.MANUAL) at the main UI.

I would recommend automatic push mode instead.

It just occurred to my mind, that in some cases when you call select(), the state is updated yes, but if there is no new activity triggered in UI by user, the change wont be visible before there is activity. You could easily verify this by adding button in UI. If select becomes visible when user click button, it is clear sign that state was correctly updated, and change was is in queue.

If you use automatic push, server will be able to send notification to UI and updates in queue are processed to UI before there is triggering user activity in UI.

https://vaadin.com/docs/v8/framework/advanced/advanced-push.html

Thanks for your Id but I fixed it.
It was a bit easier.
The currentUser is normally a user the the users list but for the selecthionModel it is a different user.
I compared the currentUser with the users in the list and selected the correct user from the list and it worked.

Done :slight_smile: