Nouras
Nouras

Reputation: 129

Angular material tree with checkboxes SelectionModel

I am trying to get the items preselected, so I used SelectionModel which works fine, but the problem happens which clicking on parent item to deselect all child, the parent item has to be clicked 3 times to get it to work.

ngOninit() {
        this.treeControl.dataNodes.forEach(node => {
          if ( node.level === 2 && res.item_ids.includes(+node.id)) {
             this.checklistSelection.toggle(node);
          }
        }); 
}

as you can see I am selecting all the items on level to that is matching a given array with item_ids, it works fine, and if all children items are selected it selects the parent as well, but when I deselect the parent item it keeps the child items selected until I ( deselect then select then again deselect) parent item.

Upvotes: 0

Views: 4072

Answers (1)

Nouras
Nouras

Reputation: 129

I found the solution by following the example provided in Angular material website, https://stackblitz.com/angular/yvdqyjdmmke?file=app%2Ftree-checklist-example.ts

I used the provided function todoItemSelectionToggle() and make sure this.checkAllParentsSelection(node); is used within the function.

Upvotes: 1

Related Questions