javacheckboxvaadintreegrid

TreeGrid with interactive checkbox


Is there a way to add checkbox to a TreeGrid? (vaadin 8.1)

I tried using below code but when I select parent node, it doesn't automatically select all its child nodes.

treeGrid.setSelectionMode(SelectionMode.MULTI);

Is there a way to achieve this?

Thank you.


Solution

  • You'll need something along the lines of this:

    TreeGrid<String> grid = new TreeGrid<>();
    grid.addSelectionListener(e ->
        grid.getSelectedItems().forEach(item ->
            grid.getTreeData().getChildren(item).forEach(grid::select))
    );
    

    This obviously doesn't cater for deselecting afterwards but is easy to change to do so.