【问题标题】:Angular material 2 change root descendants checkbox colorAngular Material 2更改根后代复选框颜色
【发布时间】:2019-07-06 03:25:18
【问题描述】:

我正在使用带有复选框的 Angular Material 2 树,我想动态更改复选框颜色。简单的方法是单击它(触发(更改)事件)并更改MatCheckboxChange.source.color。问题是,当我单击根节点时,有什么方法可以获取所有后代复选框源(更改所有后代的颜色)?示例here

模板

<mat-checkbox class="checklist-leaf-node"
              [checked]="checklistSelection.isSelected(node)"
              (change)="todoLeafItemSelectionToggle(node, $event)">{{node.item}}</mat-checkbox>

代码

todoLeafItemSelectionToggle(node: TodoItemFlatNode, event: MatCheckboxChange): void {
    event.source.color = 'warn'; // <---

    this.checklistSelection.toggle(node);
    this.checkAllParentsSelection(node);
  }

【问题讨论】:

    标签: angular angular-material2


    【解决方案1】:

    在模板中添加颜色变量

    <mat-checkbox class="checklist-leaf-node"
              [checked]="checklistSelection.isSelected(node)"
              [color]="color"
              (change)="todoLeafItemSelectionToggle(node, $event)">{{node.item}}</mat-checkbox>
    

    然后在你的javascript中

    this.color = 'primary';
    this.color = 'accent';
    this.color = 'warn';
    

    编辑:

    动态颜色[color]="checklistSelection.isSelected(getParentNode(node)) ? 'primary' : 'warn'"

    【讨论】:

    • 这不是动态变化......我想在检查parrent时更改所有后代复选框的颜色。
    • [color]="checklistSelection.isSelected(getParentNode(node)) ? 'primary' : 'warn'" 。那行得通吗?或者类似的东西(我没有测试过)?
    猜你喜欢
    • 2019-10-16
    • 2017-10-09
    • 2021-09-27
    • 2020-07-18
    • 2020-10-08
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多