How to implement selection of all tree items using selectionmodel?

417 Views Asked by At

I have a tree with a selection of items. I organized the picklist change event like this.

checklistSelection = new SelectionModel<TodoItemFlatNode>(true);
this.checklistSelection.changed.subscribe(data => {
  this.selectedEmit.emit(this.checklistSelection.selected.map(s => s.item));
});

Previously, the user could only select tree elements by clicking. And everything was fine. I have now added a "select all" button.

public SelectAllNode(): void {
 this.checklistSelection.clear();
 if (this.isCheckedAllNode) {
   for (let dataNode of this.treeControl.dataNodes) {
     this.checklistSelection.select(dataNode);
   }
 } else {
   for (let dataNode of this.treeControl.dataNodes) {
     this.checklistSelection.deselect(dataNode);
   }
 }
}

I have over 3000 items in my tree. When the user clicks this button, 3000 events happen - very impractical. Please tell me how best to organize the selection of all elements? For example, unsubscribe from an event, and at the end subscribe to it again?

1

There are 1 best solutions below

0
On

It seems that the problem went away when I did this:

public SelectAllNode(): void {
  this.checklistSelection.clear();
  if (this.isCheckedAllNode) {
    this.checklistSelection.select(...this.treeControl.dataNodes);
  } else {
    this.checklistSelection.deselect(...this.treeControl.dataNodes);
  }
}

But I don't know how good this is.