带有复选框的 Angular Material 树 - 获取选中和不确定的值

标签 angular angular-material angular-cdk angular-tree-component

我从这个例子中得到了几乎 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/

相关文章:

javascript - 响应式(Reactive)表单验证不起作用

angular - npm 运行构建 :ssr does not work in angular 8

css - 避免组件在 Angular 上重叠

Angular Material,Mat Chips Autocomplete Bug,matChipInputTokenEnd 在 optionSelected 之前执行

javascript - 专注于绝对元素

angular - *ngFor 循环遍历数组 Angular6

angular - 为 Angular Material 的 <mat-select> 组件实现搜索过滤器

angular - 使用@angular/cdk-experimental 在 Angular Material 2 表中使用虚拟滚动

angular - mat-menu 穿透 "cdk overlay fog"

html - 带有粘性标题和水平、垂直滚动条的垫表