【发布时间】:2019-05-10 03:25:03
【问题描述】:
在这里,我正在尝试使用父子和子节点上的复选框的可折叠/树结构,但我无法准确地创建它,直到从 json 中创建无序列表
{
"properties": {
"host": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
},
"information": {
"properties": {
"filetype": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
},
"author": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
},
"authorcountry": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
}
}
},
"url": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
},
"name": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
},
"instrument": {
"properties": {
"Style": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
},
"instrumentCode": {
"type": "integer"
},
"instrumentName": {
"type": "text"
},
"instrumentNumber": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text",
"fielddata": true
}
}
}
}
}
.html代码
<button class="btn btn-primary" (click)="getData()">getData</button>
<h1>ul element</h1>
<hr>
<ul class="list-group" *ngFor="let x of inf | keyvalue">
<li class="list-group-item">{{x.key}}</li>
<ng-container *ngIf="x.value.hasOwnProperty('properties')">
<ul *ngFor="let y of x.value.properties | keyvalue">
<li>
{{y.key}}
</li>
</ul>
</ng-container>
</ul>
可折叠/树结构
下面是我的 stackblitz 链接
https://stackblitz.com/edit/angular-k5tdpe
我也可以尝试插件,但插件的输入数据格式不同 angular2-tree plugin & ng2 -tree /ngx-tree 所以有什么建议
【问题讨论】:
-
虽然使用属性名称作为值可能看起来很简洁,但使用它们做任何事情变得更加困难。我建议您为树的每个节点考虑一个单一、一致的 JavaScript 结构。
{name: "mynode", data: "whatever", children: [{name: "mychild", data: "stuff"},{name: "mychild2",data: "Stuff2"}]} -
@James 我可以将我现有的数据转换为该格式吗?如果可以,您可以使用我现有的 json 数据创建一个示例。我还根据需要检查了 ng-tree 我的整个网站使用上述 json 格式的相同格式,所以只有为此我需要获取 diff 格式
标签: javascript angular twitter-bootstrap typescript twitter-bootstrap-3