Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(tree-checklist-example): root tree node correct selected state#13523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eca35f93a6e594663737ba5a94cbFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -168,10 +168,13 @@ export class TreeChecklistExample { | ||
| return flatNode; | ||
| } | ||
| /** Whether all the descendants of the node are selected */ | ||
| /** Whether all the descendants of the node are selected. */ | ||
| descendantsAllSelected(node: TodoItemFlatNode): boolean { | ||
| const descendants = this.treeControl.getDescendants(node); | ||
| return descendants.every(child => this.checklistSelection.isSelected(child)); | ||
| const descAllSelected = descendants.every(child => | ||
| this.checklistSelection.isSelected(child) | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the | ||
| ); | ||
| return descAllSelected; | ||
| } | ||
| /** Whether part of the descendants are selected */ | ||
| @@ -188,6 +191,61 @@ export class TreeChecklistExample { | ||
| this.checklistSelection.isSelected(node) | ||
| ? this.checklistSelection.select(...descendants) | ||
| : this.checklistSelection.deselect(...descendants); | ||
| // Force update for the parent | ||
| descendants.every(child => | ||
| this.checklistSelection.isSelected(child) | ||
| ); | ||
| this.checkAllParentsSelection(node); | ||
| } | ||
| /** Toggle a leaf to-do item selection. Check all the parents to see if they changed */ | ||
| todoLeafItemSelectionToggle(node: TodoItemFlatNode): void { | ||
| this.checklistSelection.toggle(node); | ||
| this.checkAllParentsSelection(node); | ||
| } | ||
| /* Checks all the parents when a leaf node is selected/unselected */ | ||
| checkAllParentsSelection(node: TodoItemFlatNode): void { | ||
| let parent: TodoItemFlatNode | null = this.getParentNode(node); | ||
| while (parent) { | ||
| this.checkRootNodeSelection(parent); | ||
| parent = this.getParentNode(parent); | ||
| } | ||
| } | ||
| /** Check root node checked state and change it accordingly */ | ||
| checkRootNodeSelection(node: TodoItemFlatNode): void { | ||
| const nodeSelected = this.checklistSelection.isSelected(node); | ||
| const descendants = this.treeControl.getDescendants(node); | ||
| const descAllSelected = descendants.every(child => | ||
| this.checklistSelection.isSelected(child) | ||
| ); | ||
| if (nodeSelected && !descAllSelected) { | ||
| this.checklistSelection.deselect(node); | ||
| } else if (!nodeSelected && descAllSelected) { | ||
| this.checklistSelection.select(node); | ||
| } | ||
| } | ||
| /* Get the parent node of a node */ | ||
| getParentNode(node: TodoItemFlatNode): TodoItemFlatNode | null { | ||
| const currentLevel = this.getLevel(node); | ||
| if (currentLevel < 1) { | ||
| return null; | ||
| } | ||
| const startIndex = this.treeControl.dataNodes.indexOf(node) - 1; | ||
| for (let i = startIndex; i >= 0; i--) { | ||
| const currentNode = this.treeControl.dataNodes[i]; | ||
| if (this.getLevel(currentNode) < currentLevel) { | ||
| return currentNode; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| /** Select the category so we can insert the new item. */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can just use
return descendants.every(...)here instead of assigning it to a constant.