I'm trying to create a table dynamically with JSON data, including the headers. I used this post to get as far as I've gotten.
My problem is that the data in my table appears completely messed up. I know in my first for each loop I should only iterate through dataMessageResults[0]
, but that gives me a syntax error.
My results.html:
<div class="play-container">
<h1>Child</h1>
<table>
<thead>
<tr *ngFor="let item of dataMessageResults">
<th *ngFor="let item of dataMessageResults | keyvalue">{{item.key}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of dataMessageResults">
<td *ngFor="let list of item | keyvalue">{{item.value}}</td>
</tr>
</tbody>
</table>
My results.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-message-results',
templateUrl: './message-results.component.html',
styleUrls: ['./message-results.component.scss']
})
export class MessageResultsComponent {
@Input() dataMessageResults: Object;
constructor() { }
}
an example of the data. Angular seems to add a number for each JSON which is resulting in the numbers in the headers.
{
"elemNb": "",
"msgID": "",
"year": "2019",
"week": "42",
"sysDatetime": "2019-10-16T11:57:34.748Z",
"airline": "EJU",
},
{
"elemNb": "",
"msgID": "",
"year": "2019",
"week": "42",
"sysDatetime": "2019-10-16T11:57:35.296Z",
"airline": "DLH",
},
{
"elemNb": "",
"msgID": "",
"year": "2019",
"week": "42",
"sysDatetime": "2019-10-16T11:59:48.599Z",
"airline": "BAW",
"tail": " ",
What am I doing wrong?