[英]Nested json which need to be display in tree structure format dynamically with the help of angular
I am seeking the below result with nested JSON
. 我正在寻找嵌套JSON
的以下结果。 I am not able to retrive/call keys and values in template dynamically. 我无法动态检索/调用模板中的键和值。 Refrence link is attached for detail veiw 附有参考链接,以了解详细信息
Angular Code 角度代码
let checklist = {
"CS": [
{
"id": "1",
"name": "A"
},
{
"id": "2",
"name": "B"
},
{
"id": "3",
"name": "C"
}
],
"Comment": [
{
"id": "1",
"name": "D"
},
{
"id": "2",
"name": "E"
},
{
"id": "3",
"name": "F"
}
]
}
<div *ngFor="let item of Checklist | Key">
{{key}}
<div>{{item}}</div>
</div>
Desired Result 所需结果
Use keyvalue
pipe to loop over Objects within template, Use some css to modify the display but a code like below will serve your need. 使用keyvalue
管道在模板内循环对象,使用一些css修改显示,但如下所示的代码将满足您的需求。
<div *ngFor="let item of checklist | keyvalue">
<div>
{{item.key}}
</div>
<div>
<p *ngFor="let eachValue of item.value">
{{eachValue.name}}
</p>
</div>
</div>
https://stackblitz.com/edit/angular-jgwk8n?file=src%2Fapp%2Fapp.component.html https://stackblitz.com/edit/angular-jgwk8n?file=src%2Fapp%2Fapp.component.html
Edit 编辑
For angular version < 6, keyvalue
pipe doesn't exist. 对于角度版本<6, keyvalue
管道不存在。 You can create your own custom pipe, maybe like: 您可以创建自己的自定义管道,例如:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'customKeyvalue',
pure: true // keyvalue pipe is actually impure, which means this value would be false
})
export class CustomKeyvaluePipe implements PipeTransform {
transform(inputOb: any): any {
let returnVal = [];
for (let eachKey in inputOb) {
returnVal.push({key: eachKey, value: inputOb[eachKey]})
}
return returnVal
}
}
Now in case your Object changes dynamically without changing its original reference then you would have to make the above pipe as impure
(pure: false). 现在,如果您的Object动态更改而没有更改其原始引用,则必须使上述管道impure
(纯净值:false)。 This has a downside of being triggered in every change detection. 这在每次更改检测中都会被触发。
https://stackblitz.com/edit/angular-jgwk8n?file=src%2Fapp%2Fapp.component.html https://stackblitz.com/edit/angular-jgwk8n?file=src%2Fapp%2Fapp.component.html
You can use Object.keys to get the keys of the object. 您可以使用Object.keys来获取对象的键。
This is your component: 这是您的组件:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
public checklist: any;
public ObjectKeys = Object.keys;
name = 'Angular';
ngOnInit() {
this.checklist = {
"CS": [
{
"id": "1",
"name": "A"
},
{
"id": "2",
"name": "B"
},
{
"id": "3",
"name": "C"
}
],
"Comment": [
{
"id": "1",
"name": "D"
},
{
"id": "2",
"name": "E"
},
{
"id": "3",
"name": "F"
}
]
};
}
}
This is the HTML: 这是HTML:
<table border="1">
<tbody>
<tr *ngFor="let key of ObjectKeys(checklist)">
<td colspan="2" style="border-right-style: solid; border-width: 1px;">
{{ key }}
</td>
<td>
<div *ngFor = "let entry of checklist[key]">
{{ entry.name }}
</div>
</td>
</tr>
</tbody>
</table>
This is the result: 结果如下:
You can add CSS to make it look better, but you get the gist now :) 您可以添加CSS以使其看起来更好,但是现在您已经掌握了要点:)
This is the StackBlitz link which you can edit. 这是您可以编辑的StackBlitz链接 。
The trick here is to use the display: inline-block
and vertical-align: top
. 这里的技巧是使用display: inline-block
和vertical-align: top
。
It's similar to xyz example. 它类似于xyz示例。
<div *ngFor="let item of checklist | keyvalue" >
<div style="display:inline-block;">
{{item.key}}
</div>
<div style="display:inline-block;vertical-align: top;">
<div *ngFor="let eachValue of item.value" >
{{eachValue.name}}
</div>
</div>
</div>
Example: https://angular-hzuexu.stackblitz.io 示例: https : //angular-hzuexu.stackblitz.io
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.