Reputation: 81
I have a JSON format and I am using primeng and want to use it a tree table structure (Html file).
JSON:
{
brinname: "Aamir",
aantalPersonen: "122",
signalenVestiging: [
{
vestiging: "Ranchi",
aantalPersonen: "102",
signalenCode: [
{
signaalCode: "4",
aantalPersonen: "15"
},
{
signaalCode: "5",
aantalPersonen: "15"
} ]
}, {
vestiging: "Bangalore",
aantalPersonen: "82",
signalenCode: [
{
signaalCode: "6",
aantalPersonen: "15"
},
{
signaalCode: "7",
aantalPersonen: "15"
} ]
} ]
},
{
brinname: "Abhinav",
aantalPersonen: "122",
signalenVestiging: [
{
vestiging: "Bangalore",
aantalPersonen: "102",
signalenCode: [ {
signaalCode: "7",
aantalPersonen: "15"
}]
} ]
Can someone explain to me how can I achieve the above request? I am getting a lot of confusion to create a tree table html structure.
Upvotes: 0
Views: 1117
Reputation: 1090
Explain: You need to clone all element's property to node
's data property by Object.keys(element).forEach
- exclude array type property (signalenVestiging
, signalenCode
). Then add elements in array type property to node
's children array. (Sorry for my bad english)
You can use below code
this.jsonData.forEach(element => {
let tmp: any = {
data: {},
children: []
};
Object.keys(element).forEach(prop => {
if (prop != 'signalenVestiging') {
tmp.data[prop] = element[prop];
} else {
element[prop].forEach(c1 => {
let tmp1: any = {
data: {},
children: []
};
Object.keys(c1).forEach(prop1 => {
if (prop1 != 'signalenCode') {
tmp1.data[prop1] = c1[prop1];
} else {
c1[prop1].forEach(c2 => {
let clone = $.extend(true, {}, c2);
tmp1.children.push({ data: clone });
});
}
});
tmp.children.push(tmp1);
});
}
});
this.data.push(tmp);
});
Demo here
Upvotes: 1