Using Vaadin 14.1.17 I’ve got a lazy loaded TreeGrid which displays all of my hierarchical data but is collapsed in the beginning.
Besides that I have a Grid which displays a list of search-results (which is based on that hierarchical data but ignores the hierarchy-level).
Problem 1: SOLVED
When I select an item of the Grid and the TreeGrid is fully expanded (manually) I want it to be selected in the TreeGrid too.
The ValueChangeListener of the Grid is triggered with the correct item but the select()-call seems to be ignored by the TreeGrid, the displayed Objects of TreeGrid and Grid are the same.
Answer: Category.java was missing an equals() and hashCode() method
Problem 2:
When I select an item of the Grid and the TreeGrid is collapsed I want to expand it until the item is visible.
What’s the best-practice here?
My sample code-base:
final HierarchicalDataProvider<Category, Void> dataProvider = new AbstractBackEndHierarchicalDataProvider<>() {
@Override
public int getChildCount(final HierarchicalQuery<Category, Void> hierarchicalQuery) {
final String parentUuid = hierarchicalQuery.getParentOptional()
.map(Category::getUuid)
.orElse(null);;
return categoryRepository.countByParentUuid(parentUuid);
}
@Override
public boolean hasChildren(final Category category) {
final String parentUuid = Optional.ofNullable(category)
.map(Category::getUuid)
.orElse(null);;
return categoryRepository.existsByParentUuid(parentUuid);
}
@Override
protected Stream<Category> fetchChildrenFromBackEnd(final HierarchicalQuery<Category, Void> hierarchicalQuery) {
final String parentUuid = hierarchicalQuery.getParentOptional()
.map(Category::getUuid)
.orElse(null);;
return categoryRepository.findByParentUuid(parentUuid).stream();
}
};
final TreeGrid<Category> lazyTree = new TreeGrid<>();
lazyTree.addHierarchyColumn(Category::getName).setFlexGrow(35).setHeader("Name");
lazyTree.addColumn(Category::getDescription).setFlexGrow(60).setHeader("Description");
lazyTree.addColumn(Category::getId).setFlexGrow(5).setHeader("ID");
lazyTree.setDataProvider(dataProvider);
final Grid<Category> searchGrid = new Grid<>();
searchGrid.addColumn(Category::getId).setHeader("ID").setFlexGrow(5);
searchGrid.addColumn(Category::getName).setHeader("Name").setFlexGrow(35);
searchGrid.addColumn(Category::getDescription).setHeader("Description").setFlexGrow(60);
// display search results independent of their hierarchy-level
searchGrid.setItems(categoryRepository.findByDescription("tools"));
searchGrid.asSingleSelect().addValueChangeListener(e -> {
lazyTree.deselectAll();
// problem 1: (isn't selecting the item even if it's expanded and visible)
// SOLVED by adding equals() and hashCode()
lazyTree.select(e.getValue());
// problem 2: what's the best way to expand the tree until the item is visible?
// (i could backtrace the parents until the root and then work my way down by expanding them?)
});
add(lazyTree, searchGrid);