angular - 使用数据 bool 值展开/折叠 Material 树(Angular 7)

标签 angular treeview angular-material angular7

我试图从它的数据源中获取一个使用 bool 值的 Material 树。我在输入时过滤 datasource.data,这会导致每次用户输入内容时树都会折叠。为了避免这种情况,我想在特定行中使用 bool 值来展开/折叠 Material 树。我已经用 Material 表成功地做到了这一点,但我无法让它与树一起工作。这是我到目前为止:

HTML 文件:

<mat-tree [dataSource]="nestedDataSource" [treeControl]="nestedTreeControl" class="example-tree">
  <mat-tree-node *matTreeNodeDef="let node">
    <li class="mat-tree-node">
      <button mat-icon-button disabled></button>
      {{node.name}}
    </li>
  </mat-tree-node>

  <mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
    <li>
      <div class="mat-tree-node">
        <button mat-icon-button
                [attr.aria-label]="'toggle ' + node.name"
                click="changeState(node)">
          <mat-icon class="mat-icon-rtl-mirror">
            {{node.expanded ? 'expand_more' : 'chevron_right'}}
          </mat-icon>
        </button>
        {{node.name}} ({{node.expanded}})
      </div>
      <ul [class.example-tree-invisible]="node.expanded">
        <ng-container matTreeNodeOutlet></ng-container>
      </ul>
    </li>
  </mat-nested-tree-node>
</mat-tree>

TS 文件:
import {NestedTreeControl} from '@angular/cdk/tree';
import {Component, Injectable} from '@angular/core';
import {MatTreeNestedDataSource} from '@angular/material/tree';
import {BehaviorSubject} from 'rxjs';

/**
 * Json node data with nested structure. Each node has a filename and a value or a list of children
 */
export class FileNode {
  childGroups: FileNode[];
  name: string;
  expanded: boolean;
}

/**
 * The Json tree data in string. The data could be parsed into Json object
 */
const TREE_DATA = [
  {
  'name': 'Group 1',
  'expanded': false,
  'childGroups': [
    {
      'name': 'Childgroup 1',
      'expanded': false,
      'childGroups': []
    },
    {
      'name': 'Childgroup 2',
      'expanded': false,
      'childGroups': [
        {
          'name': 'Child of child',
          'expanded': false,
          'childGroups': []
        }
      ]
    }
    ]
  },
    {
  'name': 'Group 2',
  'expanded': false,
  'childGroups': [
    {
      'name': 'Childgroup 1',
      'expanded': false,
      'childGroups': []
    },
    {
      'name': 'Childgroup 2',
      'expanded': false,
      'childGroups': [
        {
          'name': 'Child of child',
          'expanded': false,
          'childGroups': []
        }
      ]
    }
    ]
  },
    {
  'name': 'Group 3',
  'expanded': false,
  'childGroups': [
    {
      'name': 'Childgroup 1',
      'expanded': false,
      'childGroups': []
    },
    {
      'name': 'Childgroup 2',
      'expanded': false,
      'childGroups': [
        {
          'name': 'Child of child',
          'expanded': false,
          'childGroups': []
        }
      ]
    }
    ]
  },
]

@Component({
  selector: 'tree-nested-overview-example',
  templateUrl: 'tree-nested-overview-example.html',
  styleUrls: ['tree-nested-overview-example.css']
})
export class TreeNestedOverviewExample {
  nestedTreeControl: NestedTreeControl<FileNode>;
  nestedDataSource: MatTreeNestedDataSource<FileNode>;

  constructor() {
    this.nestedTreeControl = new NestedTreeControl<FileNode>(this._getChildren);
    this.nestedDataSource = new MatTreeNestedDataSource();
    this.nestedDataSource.data = TREE_DATA;
  }

  hasNestedChild = (_: number, nodeData: FileNode) => 
  nodeData.childGroups.length > 0;

  private _getChildren = (node: FileNode) => node.childGroups;

  changeState(node) {
    console.log(node);
    node.expanded = !node.expanded;
  }
}

Stackblitz

最佳答案

来自 Angular documentation on User Input :

To bind to a DOM event, surround the DOM event name in parentheses and assign a quoted template statement to it.



您需要做的就是改变 点击 (点击) .

DEMO

关于angular - 使用数据 bool 值展开/折叠 Material 树(Angular 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082684/

相关文章:

angular - 如何从 angular2 的下拉列表中获取旧值和新值

angular - 我如何在 angular 中使用品牌 fontawesome 图标

Angular 4路由到外部网址

mysql - 从 Angular2 组件向 node.js 发送数据

angular - 与不同的 Angular 应用程序共享 Material 主题

windows - 为什么 TVM_GETITEM 消息在 comctl32.ocx 或 mscomctl.ocx TreeView 上失败?

c# - 使用 WinForms TreeView 的递归目录列表?

c# - 具有多色 TreeNode 文本的 TreeView

css - Flexbox 和 Angular Material 选项卡

Angular 5使用 Material , Jasmine karma 测试失败,带有mat-icon