如何制作mat-tree组件Angular Material 6.0.1 [英] How to make mat-tree component Angular Material 6.0.1

查看:43
本文介绍了如何制作mat-tree组件Angular Material 6.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个基于材料角度 mat-tree 的应用程序,但是当我运行此代码时,它没有显示值,而且我没有收到任何错误,我该如何解决这个问题,以帮助我继续前进

当我们打开应用程序时,我们需要显示类名

下面我添加了我的html和组件代码

<li><div class="mat-tree-node"><button mat-icon-button matTreeNodeToggle[attr.aria-label]="'toggle' + node.filename"><mat-icon class="mat-icon-rtl-mirror">{{nestedTreeControl.isExpanded(node) ?'expand_more':'chevron_right'}}</mat-icon>

<ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)"><ng-container matTreeNodeOutlet></ng-container></mat-nested-tree-node>

组件:

const TREE_DATA = [{"class":"ece",计数":60,学生们": [{"name":"a","id":"11"},{"name":"b","id":"12"},{"name":"c","id":"13"},{"name":"d","id":"14"}]},{"class":"机械",计数":60,学生们": [{"name":"r","id":"21"},{"name":"e","id":"22"},{"name":"w","id":"23"},{"name":"q","id":"24"}]}];@Injectable()导出类文件数据库{dataChange = new BehaviorSubject([]);get data(): FileNode[] { return this.dataChange.value;}构造函数(){this.initialize();}初始化(){const dataObject = TREE_DATA;const data = this.buildFileTree(dataObject, 0);this.dataChange.next(data);}buildFileTree(obj: object, level: number): FileNode[] {return Object.keys(obj).reduce((accumulator, key) => {const 值 = obj[key];const node = new FileNode();node.filename = 键;如果(值!= null){if (typeof value === 'object') {node.children = this.buildFileTree(value, level + 1);} 别的 {node.type = 值;}}返回 accumulator.concat(node);}, []);}}/*** 带有嵌套节点的@title 树*/@成分({选择器:树状嵌套概览示例",templateUrl: 'tree-nested-overview-example.html',styleUrls: ['tree-nested-overview-example.css'],提供者:[文件数据库]})导出类 TreeNestedOverviewExample {NestedTreeControl: NestedTreeControl;嵌套数据源:MatTreeNestedDataSource;构造函数(数据库:文件数据库){this.nestedTreeControl = new NestedTreeControl(this._getChildren);this.nestedDataSource = new MatTreeNestedDataSource();database.dataChange.subscribe(data => this.nestedDataSource.data = data);}hasNestedChild = (_: number, nodeData: FileNode) =>!nodeData.type;私有_getChildren =(节点:FileNode)=>node.children;}

这是我的

当我点击 ece 时它应该是这样的

解决方案

添加了{{node :json}}下面的内容,你只需要从node对象中取出正确的数据即可.

 

I am trying to do a material angular mat-tree based app but when i run this code it is not displaying values and i am not getting any error how can i resolve this help me out to move forward

when we open app we need to show class names

below i have added my html and component code

<mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
    <li>
      <div class="mat-tree-node">
        <button mat-icon-button matTreeNodeToggle
                [attr.aria-label]="'toggle ' + node.filename">
          <mat-icon class="mat-icon-rtl-mirror">
            {{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
          </mat-icon>
        </button>

      </div>
      <ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)">
        <ng-container matTreeNodeOutlet></ng-container>
      </ul>
    </li>
  </mat-nested-tree-node>

component:

const TREE_DATA = [
{
    "class":"ece",
    "count":60,
    "students": [

            {"name":"a","id":"11"},
            {"name":"b","id":"12"},
            {"name":"c","id":"13"},
            {"name":"d","id":"14"}

    ]
},
{
    "class":"mech",
    "count":60,
    "students": [

            {"name":"r","id":"21"},
            {"name":"e","id":"22"},
            {"name":"w","id":"23"},
            {"name":"q","id":"24"}

    ]
}
];


@Injectable()
export class FileDatabase {
  dataChange = new BehaviorSubject<FileNode[]>([]);

  get data(): FileNode[] { return this.dataChange.value; }

  constructor() {
    this.initialize();
  }

  initialize() {

    const dataObject = TREE_DATA;


    const data = this.buildFileTree(dataObject, 0);


    this.dataChange.next(data);
  }


  buildFileTree(obj: object, level: number): FileNode[] {
    return Object.keys(obj).reduce<FileNode[]>((accumulator, key) => {
      const value = obj[key];
      const node = new FileNode();
      node.filename = key;

      if (value != null) {
        if (typeof value === 'object') {
          node.children = this.buildFileTree(value, level + 1);
        } else {
          node.type = value;
        }
      }

      return accumulator.concat(node);
    }, []);
  }
}

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

  constructor(database: FileDatabase) {
    this.nestedTreeControl = new NestedTreeControl<FileNode>(this._getChildren);
    this.nestedDataSource = new MatTreeNestedDataSource();

    database.dataChange.subscribe(data => this.nestedDataSource.data = data);
  }

  hasNestedChild = (_: number, nodeData: FileNode) => !nodeData.type;

  private _getChildren = (node: FileNode) => node.children;
}

here is my demo

when i open page it should come like this

when i click ece it should come like this

解决方案

It has the content below {{node :json}} added, you just need to get the correct data out of the node object.

    <button mat-icon-button matTreeNodeToggle
            [attr.aria-label]="'toggle ' + node.filename">
      <mat-icon class="mat-icon-rtl-mirror">
        {{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
      </mat-icon>
      {{node | json}}
    </button>

这篇关于如何制作mat-tree组件Angular Material 6.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆