Javascript 角度树组件json为可接受格式,并动态创建复选框或单选按钮
我有一个树状视图,使用角度树组件作为以下链接 其中数组的格式如下:Javascript 角度树组件json为可接受格式,并动态创建复选框或单选按钮,javascript,json,angular,treeview,Javascript,Json,Angular,Treeview,我有一个树状视图,使用角度树组件作为以下链接 其中数组的格式如下: nodes = [ { id: 1, name: 'root1', children: [ { id: 2, name: 'child1' }, { id: 3, name: 'child2' } ] }, { id: 4, name: 'root2', children: [
nodes = [
{
id: 1,
name: 'root1',
children: [
{ id: 2, name: 'child1' },
{ id: 3, name: 'child2' }
]
},
{
id: 4,
name: 'root2',
children: [
{ id: 5, name: 'child2.1' },
{
id: 6,
name: 'child2.2',
children: [
{ id: 7, name: 'granchild 2.2.1' }
]
}
]
}
];
[
{
"name": "root1",
"subCategory": [
{
"name": "child1",
"childCategory": []
},
{
"name": "child2",
"childCategory": []
}
]
},
{
"name": "level2",
"subCategory": [
{
"name": "level2.1",
"childCategory": []
},
{
"name": "level2.2",
"childCategory": [
{
"name": "granchild 2.2.1",
"type": "checkbox"
}
]
}
]
}
]
但是我有一个json
嵌套数组,格式如下:
nodes = [
{
id: 1,
name: 'root1',
children: [
{ id: 2, name: 'child1' },
{ id: 3, name: 'child2' }
]
},
{
id: 4,
name: 'root2',
children: [
{ id: 5, name: 'child2.1' },
{
id: 6,
name: 'child2.2',
children: [
{ id: 7, name: 'granchild 2.2.1' }
]
}
]
}
];
[
{
"name": "root1",
"subCategory": [
{
"name": "child1",
"childCategory": []
},
{
"name": "child2",
"childCategory": []
}
]
},
{
"name": "level2",
"subCategory": [
{
"name": "level2.1",
"childCategory": []
},
{
"name": "level2.2",
"childCategory": [
{
"name": "granchild 2.2.1",
"type": "checkbox"
}
]
}
]
}
]
我的疑问如下:
- 如何将json转换为角度树组件可接受的所需数组格式
- 正如您所看到的,类型是checkbox
。所以孙子2.2.1应该是一个复选框,如果它是单选按钮,它应该是单选的{“name”:“granchild 2.2.1”,“type”:“checkbox”}
请指导我…您可以使用此代码(开始时id=1)将json转换为您的数组
=[
{
“名称”:“root1”,
“子类别”:[
{
“姓名”:“child1”,
“儿童类别”:[]
},
{
“姓名”:“child2”,
“儿童类别”:[]
}
]
},
{
“名称”:“2级”,
“子类别”:[
{
“名称”:“2.1级”,
“儿童类别”:[]
},
{
“名称”:“2.2级”,
“儿童类别”:[
{
“名称”:“granchild 2.2.1”,
“键入”:“复选框”
}
]
}
]
}
]
设id=1;
a、 地图(x=>(
x、 id=id,
id++,
x、 子类别映射(y=>(
y、 id=id,
id++,
(y.childCategory.length?y.children=y.childCategory:0),
y、 map(z=>(z.id=id,id++),
删除y.childCategory
)),
(x.subCategory.length?x.children=x.subCategory:0),
删除x.子类别
));
控制台日志(a)代码>您的输出所需的数组不包含我们可以放置childCategory类型的位置-显示您可以更新它吗?显示类型
应该放置在哪里?谢谢老兄。你知道第二部分吗?在角度树组件中动态创建复选框或单选按钮?请检查一下,帮帮我,兄弟。你能帮我一下吗。