I am hitting an api that can will return different JSON's, each meaning a different table.
This is the generic HTML in my compenent.html
<table mat-table [dataSource] = "cols" class="mat-elevation-z8">
<ng-container *ngFor="let col of cols" [matColumnDef]="col">
<th mat-header-cell *matHeaderCellDef> {{ col }} </th>
<td mat-cell *matCellDef="let element"> {{ element[col] }} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
The data I'm getting in my component class
getData(){
this.requestService.getTableData().subscribe(
data => {
this.arrData = data as string [];
console.log(this.arrData['requestType']);
},
error => {
console.log (error.message);
}
)
this.cols = JSON.parse(this.arrData['responsePayload']);
}
the JSON Data I'm finally getting looks like this
[
{
"Company": "Alfreds Futterkiste",
"Contact": "Maria Anders",
"Country": "Germany"
},
....
]
this.cols is my data source for the mat-table and it is populating correctly as an array with 6 entries. I need to make a mat-table out of this, but I keep getting this error
requestHandler.html:6 ERROR TypeError: name.replace is not a function at MatColumnDef.set name [as name] (table.js:175) at updateProp (core.js:32188)
at
<ng-container *ngFor="let col of cols" [matColumnDef]="col">
Can someone point me in the right direction / tell me where I'm going wrong?
Also, if something isn't clear in the question please let me know I'll try explaining again.
Note: I believe there is a solution in making an interface for the JSON data I'm getting, but I will end up making a slightly static HTML for each type of table. I want to keep it generic. Something like a dynamic interface (which can't be done, I think.) This is kinda cool though, but I need to be able to do this in runtime.