I’m having trouble with the TreeGrid component in multi-select mode. When I select all items with treeGrid.asMultiSelect().select(projects), everything seems fine initially, the treeGrid.getSelectedItems() method shows the correct selections, but the UI only shows selected the root elements or the child elements.
When I expand any node, the visual selection gets messed up. The selected items might appear deselected, even though they are still selected internally.
Has anyone seen this issue? How can I make sure the visual selection stays consistent with the actual selection after expanding nodes?
Hello,
I recreated the Issue in a small project with a Project class and a view with the TreeGrid. Is there something I’m missing in the code?
Project class
public class Project {
Integer id;
List<Project> childrens;
public Project(Integer id) {
super();
this.id = id;
childrens = new ArrayList<>();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Project> getChildrens() {
return childrens;
}
public void setChildrens(List<Project> childrens) {
this.childrens = childrens;
}
}
View
public class MyViewView extends VerticalLayout {
Project p1 = new Project(1);
Project p2 = new Project(2);
Project p3 = new Project(3);
Project p11 = new Project(11);
Project p12 = new Project(12);
Project p121 = new Project(121);
Project p21 = new Project(21);
public MyViewView() {
p1.setChildrens(List.of(p11, p12));
p2.setChildrens(List.of(p21));
p12.setChildrens(List.of(p121));
List<Project> projects = List.of(p1, p2, p3, p11, p12, p21, p121);
List<Project> parents = List.of(p1, p2, p3);
TreeGrid<Project> projectsGrid = new TreeGrid<>();
projectsGrid.setSelectionMode(SelectionMode.MULTI);
projectsGrid.addHierarchyColumn(p -> p.getId());
projectsGrid.setItems(parents, Project::getChildrens);
projectsGrid.asMultiSelect().select(projects);
projectsGrid.expand(projects);
add(projectsGrid);
}
}
I tried your example in v24.4 and it works without any issue. All nodes are selected, regardless whether you expand / collapse them. Implementing equals/hashcode was not necessary, but is good practice in any case.