【问题标题】:Angular Material Tree - Different parent/child typesAngular Material Tree - 不同的父/子类型
【发布时间】:2019-07-26 05:35:44
【问题描述】:
有没有办法利用材料树来表示父/子数据结构,其中子数据结构与父数据形状不同?我对这个组件的经验似乎表明数据结构需要是相同类型的递归。但是,我想用它来表示诸如 Orders(父级)和 Order Items(子级)之类的结构。这似乎不是受支持的用例。对吗?
自从 v2 推出以来,我一直在使用 Angular,并且从一开始我就一直在使用 Angular Material。在我看来,Tree 组件似乎是最繁琐且代码最难采样的组件。
【问题讨论】:
标签:
angular
angular-material
【解决方案1】:
我遇到了同样的问题,我选择的解决方案是为树组件制作一个扁平结构。结构具有来自父节点和子节点的所有字段(如果在父节点上使用模型,则子字段保持为空并且在子节点上相反)。似乎工作得很好。
发件人:
export interface ParentModel{
id: number;
name: string;
child: ChildModel[];
}
export interface ChildModel{
id: number;
symbolName: string;
description: string;
}
到:
export interface TreeModel {
id: number;
name: string;
symbolName: string;
description: string;
}
【解决方案3】:
刚遇到同样的问题,由于没有可用的示例,我想出了以下解决方案。
/**
* Any class that implements toString is now usable inside a tree
*/
export interface IString {
toString : () => string;
}
/**
* Node for to-do item
*/
export interface Node {
children? : Node[];
item : IString;
}
/** Flat to-do item node with expandable and level information */
export interface FlatNode {
item : IString;
level : number;
expandable : boolean;
}
查看 Stackblitz:https://stackblitz.com/edit/angular-rwur5a