Reputation: 1590
How to disable unselecting Grid
row in Vaadin 7, but with permission to select another row using keyboard or mouse click?
Grid grid = new Grid(container);
grid.setSelectionMode(Grid.SelectionMode.SINGLE);
For example this is possible for older Table
component - SO answer. But I widely use Grid
so I want use it also in this case.
Upvotes: 2
Views: 2339
Reputation: 1704
In Vaadin 8 you may use:
grid.setSelectionMode(SINGLE);
((SingleSelectionModel) grid.getSelectionModel()).setDeselectAllowed(false);
Upvotes: 2
Reputation: 1590
I found one interesting solution, but unfortunately not perfect.
To prevent deselect row we could write a SelectionListener
and put there some logic:
grid.setSelectionMode(Grid.SelectionMode.SINGLE);
grid.addSelectionListener(event -> {
Set<Object> selected = event.getSelected();
if (selected == null || selected.isEmpty()) {
Set<Object> removed = event.getRemoved();
removed.stream().filter(Objects::nonNull).forEach(someGrid::select);
}
});
So assuming single selection mode, if current selection is empty, then previous selected row should be selected again. But if current selection isn't empty it means that somebody select another row - this doesn't require any action.
It is cool but not enough - every click (selection) cause http call and network transmission. This is disadvantage.
Upvotes: 2