Immanuel
Immanuel

Reputation: 11

How do I preselect checkboxes in mat-tree?

I am trying to preselect checkboxes of this mat-tree example: https://stackblitz.com/edit/material-tree-checklist. For some reason I can't preselect checkboxes at the initalization of the component.

I am trying to combine two rxjs observables with combineLatest. First one contains data nodes for the mat-tree. Second one contains the data nodes which should be preselected. This is my not-working solution: https://stackblitz.com/edit/material-tree-checklist-rj4ryd?file=app/app.component.ts

Mat-tree itself is working with this.allData$, but checking selected checkboxes at the initalization of the component with the this.selectedData$ does not work.

  combineLatest(this.allData$, this.selectedData$)
      .subscribe(val => {
        this.dataSource.data = val[0]
        val[1].map(x => this.nodeSelectionToggle(x))
   }
  )

Upvotes: 1

Views: 2587

Answers (1)

user2216584
user2216584

Reputation: 5602

As per your code, you are trying to select GameNode instance [from selectedData] which is not the same instance in allData. Notice different new GameData. Both are referring to different memory locations.

You must select the GameNode instance from the allData. Try changing your combineLatest like this:

combineLatest(this.allData$, this.selectedData$)
      .subscribe(val => {
        const data = val[0];
        this.dataSource.data = data;
        this.nodeSelectionToggle(data[1]);
      }
     );

Working stackblitz: https://stackblitz.com/edit/material-tree-checklist-re96eq?file=app/app.component.ts

Hope it helps.

Upvotes: 2

Related Questions