我从这个例子中得到了几乎 1 对 1 的树:https://stackblitz.com/angular/nnxeaxmrdob?file=src%2Fapp%2Ftree-checklist-example.ts
我需要的是获取所有选定的值和那些不确定的值。我知道,所有选定的值都保存在 checklistSelection 变量中,但是问题是当选择整个子节点时,并且我有父级和子级数组,但是虽然只选择了一些子级,但我没有 parent 。
那么,我如何获取选定且不确定的值?
最佳答案
在示例中,您可以使用
const partial=this.treeControl.dataNodes
.filter(x=>this.descendantsPartiallySelected(x))
console.log(this.checklistSelection.selected,partial)
哪里(*)
descendantsPartiallySelected(node: TodoItemFlatNode): boolean {
const descendants = this.treeControl.getDescendants(node);
const result = descendants.some(child => this.checklistSelection.isSelected(child));
return result && !this.descendantsAllSelected(node);
}
(*)您在示例中还没有这个函数
关于带有复选框的 Angular Material 树 - 获取选中和不确定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58880625/