【发布时间】:2021-04-16 08:37:44
【问题描述】:
我有这个代码:
const TREE_DATA_JSON = {
idManager: 1,
label: 'Manager',
descendants: [
{
idManager: 2,
label: 'Des 1',
areaManagers: [
{
idManager: 3,
label: 'Manager 1'
},
{
idManager: 4,
label: 'Manager 2'
},
]
}
]
};
@Injectable()
export class ChecklistDatabase {
dataChange = new BehaviorSubject<TodoItemNode[]>([]);
get data(): TodoItemNode[] { return this.dataChange.value; }
constructor() {
this.initialize();
}
initialize() {
// Build the tree nodes from Json object. The result is a list of `TodoItemNode` with nested
// file node as children.
const data = this.buildFileTree(TREE_DATA_JSON, 0);
// Notify the change.
this.dataChange.next(data);
}
/**
* Build the file structure tree. The `value` is the Json object, or a sub-tree of a Json object.
* The return value is the list of `TodoItemNode`.
*/
buildFileTree(obj: object, level: number): TodoItemNode[] {
return Object.keys(obj).reduce<TodoItemNode[]>((accumulator, key) => {
const value = obj[key];
const node = new TodoItemNode();
node.item = key;
if (value != null) {
if (typeof value === 'object') {
node.children = this.buildFileTree(value, level + 1);
} else {
node.item = value;
}
}
return accumulator.concat(node);
}, []);
}
} 但我有这样的树视图:
您知道如何修改 buildFileTree 吗?我得到的树:https://stackblitz.com/angular/gabkadkvybq?file=app%2Ftree-checklist-example.ts.
提前感谢您的留言,对不起我的英语
我想展示这样的东西:
- 经理
- 设计1
- 经理 1
- 经理 2
- 设计1
【问题讨论】:
-
您好,请告诉我们您的预期输出。由于您共享的 Stackbliz 示例按您共享的图像中的预期工作。
-
您似乎已经链接了 Angular Material 团队提供的股票示例,没有进行任何修改。请先尝试自己实现该要求。然后,您可以在遇到障碍时发布问题。
-
嗨@KIsdskldsd,我想你应该使用
FlatTree而不是NestedTree请看看这个Example -
@HimanshuSaxena 我的代码:stackblitz.com/edit/angular-ivy-xdflat
-
@MichaelD 我添加了我的堆栈闪电战:stackblitz.com/edit/angular-ivy-xdflat
标签: javascript angular angular-material