TreeGrid UI inconsistent in multi-select mode

Hello everyone,

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?

Thanks!

1 Like

Sounds like a problem with your hash code / equals implementation of your object in the tree

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);
  }

}

Result
imagen

Try to implement equals and hash code only based on the ID of the Project.

I tried that and also implemented hash and equals with all fields and issue persists

@Matti Do you happen to know if this behavior is normal for the Tree grid? I’m not using it really often…

Does it change anything if you first expand and then select?

Nop, and If I don’t expand it and then click to open it behaves like the video at the beginning. Roots are deselected (only visually)

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.

Which version are you using?

4 Likes

I was using v24.3.3, tried it with v24.4 and it works.
Thank you