2

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.

Community
  • 1
  • 1
jsosnowski
  • 1,560
  • 3
  • 26
  • 56

2 Answers2

2

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.

jsosnowski
  • 1,560
  • 3
  • 26
  • 56
2

In Vaadin 8 you may use:

grid.setSelectionMode(SINGLE);
((SingleSelectionModel) grid.getSelectionModel()).setDeselectAllowed(false);
Bruno Eberhard
  • 1,624
  • 16
  • 22