I have created table from the below mentioned JSON which works fine. I have certain condition that needs to be handled. the function which i used is also mentioned here.I also attached output image for the same.Help for the same is highly appreciated… Thanks in advance
Conditions :
- if email row is empty need to remove that particular row.
- Let’s say value2 has one value in email, in that case it should be displayed.
JavaScript
x
148
148
1
2
3
rows = [];
4
5
generateTable() {
6
if (!this.data) {
7
return;
8
}
9
10
this.rows.push([
11
{
12
text: this.data.e_o_name,
13
rowspan: 0
14
}
15
]);
16
let maxRowSpan = 0;
17
18
this.data.matching_details.forEach((detail, i) => {
19
const elemRowSpan = Math.max(detail.matching_attributes.length, 1);
20
maxRowSpan += elemRowSpan;
21
22
if (i > 0) {
23
this.rows.push([])
24
}
25
this.rows[this.rows.length - 1].push({
26
text: detail.me_value,
27
rowspan: elemRowSpan
28
});
29
30
detail.matching_attributes.forEach((attr, j) => {
31
if (j > 0) {
32
this.rows.push([])
33
}
34
35
const mail = attr.me_list[0];
36
this.rows[this.rows.length - 1].push(
37
{
38
text: attr.me_name,
39
rowspan: 1
40
},
41
{
42
text: mail.me_email_list.map(({ me_value }) => me_value).join(', '),
43
rowspan: 1
44
},
45
{
46
text: mail.me_percent,
47
rowspan: 1
48
}
49
);
50
})
51
});
52
this.rows[0][0].rowspan = maxRowSpan;
53
}
54
55
```
56
#Josn : #
57
```
58
59
{
60
"e_id":"1234",
61
"e_o_name":"Contact_info",
62
"matching_details":[
63
{
64
"me_value":"value1",
65
"matching_attributes":[
66
{
67
"me_id":"1234",
68
"me_name":"28 sai",
69
"me_list":[
70
{
71
"me_type":"Email ID",
72
"me_email_list":[
73
{
74
"me_value":"a@gmail"
75
},
76
{
77
"me_value":"b@gmail"
78
}
79
],
80
"me_percent":"100"
81
}
82
]
83
},
84
{
85
"me_id":"5678",
86
"me_name":"29 meena",
87
"me_list":[
88
{
89
"me_type":"Email ID",
90
"me_email_list":[
91
{
92
"me_value":"c@gmail.com"
93
},
94
{
95
"me_value":",d@gmail.com"
96
}
97
],
98
"me_percent":"100"
99
}
100
]
101
}
102
]
103
},
104
{
105
"me_value":"value2",
106
"matching_attributes":[
107
{
108
"me_id":"1234",
109
"me_name":"rimzim",
110
"me_list":[
111
{
112
"me_type":"Email ID",
113
"me_email_list":[
114
{
115
"me_value":"p@gmail"
116
},
117
{
118
"me_value":"q@gmail"
119
}
120
],
121
"me_percent":"100"
122
}
123
]
124
},
125
{
126
"me_id":"5678",
127
"me_name":"ranu",
128
"me_list":[
129
{
130
"me_type":"Email ID",
131
"me_email_list":[
132
{
133
"me_value":"t@gmail.com"
134
},
135
{
136
"me_value":",u@gmail.com"
137
}
138
],
139
"me_percent":"100"
140
}
141
]
142
}
143
]
144
}
145
]
146
}
147
148
Advertisement
Answer
Seems like you want to put in column (attr) level validations, so in the html while looping through it you will need to implement the checks
https://stackblitz.com/edit/angular-zm1ap1?file=src/app/app.component.html
JavaScript
1
26
26
1
<table>
2
<tbody>
3
<tr>
4
<th>contact</th>
5
<th>ty</th>
6
<th>ed</th>
7
<th>mail</th>
8
<th>percent</th>
9
</tr>
10
<tr *ngFor="let row of rows">
11
<!-- check if row is empty or could add additional check such
12
row[3].text (email) is true
13
-->
14
<ng-container *ngIf='row && row.length > 0'>
15
<td [attr.rowspan]='row[0].rowspan'>{{row[0].text}}</td>
16
<td *ngIf='row.length > 1' [attr.rowspan]='row[1].rowspan'>{{row[1].text}}</td>
17
18
<td *ngIf='row.length > 2' [attr.rowspan]='row[2].rowspan'>{{row[2].text}}</td>
19
<td *ngIf='row.length > 3' [attr.rowspan]='row[3].rowspan'>{{row[3].text}}</td>
20
<td *ngIf='row.length > 4' [attr.rowspan]='row[4].rowspan'>{{row[4].text}}</td>
21
</ng-container>
22
</tr>
23
24
</tbody>
25
</table>
26