Allow selection of only leaf node of tree[table]

Since tree hierarchy is not available for Lookup, I am trying to use Tree as lookup field but I only want to allow selection of leaf nodes. How can I achieve this. I believe similar solution should work for TreeTable also.

Hello!

I think the only way to select only leaf nodes is to check whether the entity contains children or not. For instance, subscribe to SelectionEvent and check selected entities. If an entity has children remove it and reselect items:

@Inject
private TreeTable<Directory> directoriesTable;

private TreeTableItems<Directory> tableItems;

@Subscribe
public void onAfterShow(AfterShowEvent event) {
    tableItems = (TreeTableItems<Directory>) directoriesTable.getItems();
}

@Subscribe("directoriesTable")
public void onDirectoriesTableSelection(Table.SelectionEvent<Directory> event) {
    Set<Directory> reselect = null;
    for (Directory directory : event.getSelected()) {
        if (tableItems.hasChildren(directory.getId())) {
            if (reselect == null) {
                reselect = new HashSet<>(event.getSelected());
            }
            reselect.remove(directory);
        }
    }
    if (reselect != null) {
        directoriesTable.setSelected(reselect);
    }
}
2 Likes