【问题标题】:Angular Material Tree with checkboxes - get checked and indeterminated value带有复选框的角材质树 - 获得检查和不确定的值
【发布时间】:2020-03-11 19:39:55
【问题描述】:

我从这个例子中得到了几乎 1 比 1 的树:https://stackblitz.com/angular/nnxeaxmrdob?file=src%2Fapp%2Ftree-checklist-example.ts

我需要的是获取所有选定的值和不确定的值。 I know, that all selected values are hold in checklistSelection variable, however the problem is when the whole child node is selected, and I've got array of parents and children, but while only some children are selected, then I don't have父母。

再一次,我如何获得选择和不确定的值?

【问题讨论】:

  • 问题是什么?用户界面按预期工作
  • 是的,用户界面有效。但是我如何获得被选中且不确定的对象。
  • 嗯,你有this.checklistSelection.selected,它保持选中状态。如果不解释扁平树的工作原理,很难回答。尝试将您的问题拆分为其他几个问题
  • 是的,我确实有 this.checklistSelection.selected,但是这个数组不包含不确定复选框的值。我的问题在于不一致,因为当所有子项都被选中时,父项也被选中,并且在this.checklistSelection.selected中,但是如果只选择了一些子项,则父项在this.checklistSelection.selected中不存在,因为它状态为“不确定”未选中。
  • 它包含所有选定的节点。它们都排成一列并按顺序排列。 0 级父级,1 级子级

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


【解决方案1】:

在示例中,您可以使用

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);
  }

(*)你在例子中还有这个功能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 2016-07-04
    • 2018-12-01
    • 1970-01-01
    • 2018-09-15
    • 2021-03-16
    相关资源
    最近更新 更多