angular - How to create a mat-tree from firebase data in angular
问题描述
I have data that comes from firebase (firecloud) with this structure:
{ name: name of node parent: parent_node }
i whish to build a tree in angular, however i cant manage how to input this into the structure that angular component tree uses.
¿Is there a way to inject this data directly into the mat-tree?
解决方案
Refer the angular material documentation for mat-tree https://material.angular.io/components/tree/overview . To convert the incoming data to a structure most apt for looping through mat-tree nodes as mentioned in the docs:
if you have an array structure like
const incomingData = [
{ name: name of node parent: parent_node },
{ name: name of node parent: parent_node }
]
let treeData = []
let parentNodes = incomingData.map(el=>el.parent);
parentNodes.forEach(parent=>{
let children = incomingData.filter(el=>el.parent==parent).map(el=>el.name)
parent["children"] = [] ;
children.forEach(child=>{
parent.children.push({"name":child})
})
treedata.push({"name":parent.name});
})
推荐阅读
- meetup - Mac 应用程序在会议应用程序中共享屏幕区域
- javascript - 保持选定的菜单处于活动状态
- css - 如何在css中使用非扇区的唯一类型
- spring - Spring RestTemplate 在运行时更改 URL 后开始返回 405 METHOD_NOT_ALLOWED
- sql - 如何根据 10 分钟内的日期和时间列对数据进行分组和选择?
- transpose - 将某些列更改为行,但其余部分保持不变
- python - python烧瓶URL错误:'在服务器上找不到请求的URL。如果您手动输入了 URL,请检查您的拼写并重试。
- java - 如何修复 NullPointerException?- java双向链表
- django - 遍历多对多关系以添加到 Django DetailView 中的上下文
- java - 如何使用对象将数据添加到 JavaFX 中的空表视图中?