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.
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.
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);
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
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.
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
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!