角度6物质树

时间:2018-06-12 04:43:16

标签: angular angular-material

我正在尝试根据docs做一个物质角树,但似乎遇到了一个我似乎无法诊断的错误。 Chromes在我的HTML的第8行给出了Cannot read property 'children' of undefined的错误。似乎树被加载到nestedDataSource就好了,但它不会显示任何内容。不可否认,我不完全确定Angular Material树是如何工作的,所以我可能搞砸了html。

角度6.01

类别-picker.component.html:

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

  <mat-nested-tree-node *matTreeNodeDef="let node; when: node.children.length">
    <li>
      {{node.category.name}}
      <ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)">
        <ng-container matTreeNodeOutlet></ng-container>
      </ul>
    </li>
  </mat-nested-tree-node>
</mat-tree>

类别-picker.component.ts:

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import {NestedTreeControl} from '@angular/cdk/tree';

import {MatSelectModule} from '@angular/material';
import {MatTreeNestedDataSource} from '@angular/material/tree';

import { DataBrokerService } from '../data-broker.service';
import { Category } from '../datatypes'
import { observable, of } from 'rxjs';

export class CategoryNode {
  children: CategoryNode[];
  category: Category;
}


@Component({
  selector: 'app-category-picker',
  templateUrl: './category-picker.component.html',
  styleUrls: ['./category-picker.component.scss']
})
export class CategoryPickerComponent implements OnInit {

  // Does not follow main/sub category - just has a simple category!
  @Input() categoryID: number;
  @Output() categoryIDChange:EventEmitter<number> = new EventEmitter<number>();

  nestedTreeControl: NestedTreeControl<CategoryNode>;

  nestedDataSource: MatTreeNestedDataSource<CategoryNode>;

  constructor(
    private dataBrokerService: DataBrokerService
  ) { 
    this.nestedTreeControl = new NestedTreeControl(node => of(node.children));
    this.nestedDataSource = new MatTreeNestedDataSource();
  }

  ngOnInit() {
    this.dataBrokerService.getCategories().subscribe(categories => this.nestedDataSource.data = this.buildTree(categories));
  }

  onChange(){
    this.categoryIDChange.emit(this.categoryID);
  }

  private buildTree(categories:Category[]):CategoryNode[]{
    console.log("tree");

    let tree:CategoryNode[] = [];
    let map: {[s:number]: CategoryNode} = {};

    // Build an index of the nodes
    categories.forEach(cat => {
      map[cat.id] = {children:[], category:cat};
    });

    // Start adding nodes to tree 
    for (let key of Object.keys(map)){
      let catNode = map[+key];

      if (map[catNode.category.parent_id]){
        // Add it to the parent
        map[catNode.category.parent_id].children.push(catNode);
      }
      else{
        // Add it to root
        tree.push(catNode);
      }
    };
    console.log(tree);
    return tree;
  }
}

2 个答案:

答案 0 :(得分:5)

事实证明我错误地实施了&#39; SELECT * FROM chemical WHERE name LIKE '%copper%' ORDER BY INSTR(name, 'copper') ASC &#39; when的功能。在我的HTML中,我需要将MatTreeNodeDef更改为when: node.children.length,在.ts文件中定义为when: hasNestedChild。换句话说,是一个必需参数,它带有两个输入(数字和节点)的函数。

答案 1 :(得分:0)

category-picker.component.html: <mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">

category-picker.component.ts: hasNestedChild=(_: number, nodeData) => {return nodeData.children.length}