'How to reset ngx-treeview
I'm using plug ngx tree view version 1.2.5 for tree view I need to reset the tree view or unchecked the all selected tree items I tried to do it by moving initial data to the item variable
Solution 1:[1]
I'm using this code to check/uncheck all items:
itemsChecked(boolChecked: boolean) {
this.trvComponent.items.forEach(element => {
element.checked = boolChecked;
element.children.forEach(elem => {
elem.checked = boolChecked;
});
});
}
Note: this is for a 2 level treeview. if you have more than 2 levels change the code.
Before using this code add a ViewChild decleration to Export Class section like this:
@ViewChild(TreeviewComponent, { static: true }) trvComponent: TreeviewComponent;
Solution 2:[2]
You can lever the onAllCheckedChange() api. You just need to programmatically uncheck the 'All' selector.
// get a reference to the TreeViewComponent
@ViewChild(TreeViewComponent, {Static: true}) tree: TreeViewComponent;
// reset the 'All' checked flag
// and trigger the change handler
resetSelection() {
this.tree.allItem.checked = false;
this.tree.onAllCheckedChange();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Valy Dia |
| Solution 2 | Linda |
