Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Programmatically select rows in a grid
Hi
I need to programmatically select rows in a MultiSelect-capable grid.
I did it like this:
final MultiSelectionModel multiselectionModel = (MultiSelectionModel) myGrid.getSelectionModel();
multiselectionModel.deselectAll();
multiselectionModel.select(getSelectedIds());
The getSelectedIds() method returns a Set of the select id's as defined in the datasource belonging to the grid.
I changed the last line to setSelected(...).
In both variants no rows are selected = no checkboxes are set.
I'm working on Vaadin 7.6-SNAPSHOT.
Thanks for your help.
Clau
Hi Claus,
Is it possible that the second line of your code deselects all rows, so then there are no rows returned by getSelectedIds()?
-Olli
The second line has to delete all the previous selections, because another part could have changed them.
The list of the currently selected IDs is returned by the getSelectedIds() method.
I created a selection with the following code:
Grid grid = new Grid();
Set<Report> reports = getReports(); // Report is just a Java bean
BeanItemContainer<Report> reportContainer = new BeanItemContainer<Report>(
Report.class, reports);
grid.setContainerDataSource(reportContainer);
MultiSelectionModel msm = new MultiSelectionModel();
grid.setSelectionModel(msm);
Iterator<Report> iter = reports.iterator();
// select two objects from the Set
msm.select(iter.next(), iter.next());
and it seems to work as it should, two items in the grid are selected.
EDIT: it also works if I change the last line to the following:
Set<Report> three = new HashSet<Report>();
three.add(iter.next());
three.add(iter.next());
three.add(iter.next());
msm.select(three);
Strange, this looks much like my code apart from the Iterator part. I'll test that ...
I see a difference: You refer to complete Report objects, while my code relies on id's.
I'll rewrite my code to adapt your approach....
I can't doublicate your approach, because I need BeanItemContainer and the data's id to identify it.
But it should work with IDs (?).
Did you do your example using 7.6-SNAPSHOT? It might be a bug in the curent beta.
Yes, it should work with ids. MultiSelectionModel's select() method will fail with an IllegalArgumentException if the paramer is not a proper id in the container:
if (!getParentGrid().getContainerDataSource().containsId(itemId)) {
throw new IllegalArgumentException("Given item id (" + itemId
+ ") does not exist in the container");
}
or if the input is null:
if (itemIds == null) {
throw new IllegalArgumentException("itemIds may not be null");
}
I tried also with 7.6-SNAPSHOT, with the same results.
Can you try something like the following:
final MultiSelectionModel multiselectionModel = (MultiSelectionModel) myGrid.getSelectionModel();
Set selectedIds = getSelectedIds();
System.out.println(
"Amount of selected ids: " + selectedIds.size());
multiselectionModel.deselectAll();
multiselectionModel.select(selectedIds);
System.out.println(
"Amount of selected rows in grid: " + myGrid.getSelectedRows().size());
Hi Olli
Thanks for your example. Do you refer to a particular implementation of getSelectedIds() or just a generic one returning the list of selected ids?
The number of select rows can be misleading in the context of the application, because I've to synchronize two ways to multi select by hand (one is the grid-based multi selection, the other is a custom implementation.
That's why I have to deselectAll() before.
The only thing, that I have to do is set the multi selection of a grid programatically. When debugging, I see those items being selected, but the checkboxes in the grid aren't set by the state change of the MultiSelectionModel.
Regards
Claus
Hi,
have you tried sorting the grid after changing the state of the selection model? That should force a redraw of the grid, so you can be guaranteed that everything is as up to date as possible.
Otherwise, you'll need to look further into your own code. From my limited trials, changing the selection model does reflect into the UI, but obviously you're doing something different if it's not working for you.
-Olli
Hi
that's a good idea. I'll try that.
Is there any way to force a redraw?
Claus
Hi Olli
reordering the grid, doesn't solve the issue.
Claus
Hi Olli
I suspect, that the issue is caused by the use of BeanContainer as opposed to BeanItemContainer (as in your example).
The handling of BeanItems based on their hashcode or their given id seems to be different. When I debug into the code, I see the right item ids, but the GUI isn't updated.
We have to use the BeanContainer and Vaadin 7.6 in this project, because we need several new Vaadin features.
There is no way back ;-)
Claus
Hi,
Just read your discussion and investigated a bit of Grid's code. Seems that there is a case where it does not force the selected/deselected rows to update to the client. I added a ticket for it and made a quick fix. How ever, the original example using deselectAll and select should work, but apparently it doesn't..
Can you create a quick vanilla Vaadin UI where we can see this issue happening? It would speed up the debugging process quite a lot. Thank you for your report!
//Teemu
Hi Teemu
I'm on a quite tight schedule, but I'll create a quick example to show the behaviour.
Thanks for your support!
Claus
The issue with setSelected not updating has been fixed and merged. It should be present tomorrow in a 7.6-SNAPSHOT.
While further evaluating the multi-selection behaviour I found the following.
This code works as excepted:
Grid grid = new Grid();
Set<Report> reports = getReports(); // Report is just a Java bean
BeanContainer<String, Report> reportContainer = new BeanContainer<>(Report.class);
reportContainer.setBeanIdProperty("id");
reportContainer.addAll(reports);
grid.setContainerDataSource(reportContainer);
MultiSelectionModel msm = new MultiSelectionModel();
grid.setSelectionModel(msm);
msm.select("2", "3");
layout.addComponent(grid);
The following doesn't select the reports:
Grid grid2 = new Grid();
grid2.setSelectionMode(Grid.SelectionMode.MULTI);
Set<Report> reports2 = getReports(); // Report is just a Java bean
BeanContainer<String, Report> reportContainer2 = new BeanContainer<>(Report.class);
reportContainer2.setBeanIdProperty("id");
reportContainer2.addAll(reports2);
grid2.setContainerDataSource(reportContainer2);
MultiSelectionModel msm2 = (MultiSelectionModel)grid.getSelectionModel();
msm2.setSelected("2", "3");
layout.addComponent(grid2);
The only difference between these two is the location of the SelectionMode definition.
Claus Lüthje: The only difference between these two is the location of the SelectionMode definition.
I see another one... You are doing a select in the first example vs a setSelected in the second one. Although I think it's a typo ;)
I wonder if those of you with larger brains (= more coding experience) could tell me if I'm on the right track here. My inability to replace pre-existing selections in a Grid is blocking migration to Vaadin 8. This problem looks very much like the one you're discussing in this thread.
Steve Demy: I wonder if those of you with larger brains (= more coding experience) could tell me if I'm on the right track here. My inability to replace pre-existing selections in a Grid is blocking migration to Vaadin 8. This problem looks very much like the one you're discussing in this thread.
I have the same problem with vaadin 8 Tree:
dbTree.setDataProvider(new TreeDataProvider(dbData));
dbTree.getSelectionModel().select(value);
the select is called before the Tree is filled up...