I have below json and want to render in material table in angular
var res = '[
{
"DynamicName": "xyz",
"DynamicLevel": "xyz",
"DynamicLevel": "xyz",
"DynamicLevel": "xyz",
"dynamic": "xyz"
},
{
"DynamicName": "xyz",
"DynamicLevel": "xyz",
"DynamicLevel": "xyz",
"DynamicLevel": "xyz",
"dynamic": "xyz"
},
{
"DynamicName": "xyz",
"DynamicLevel": "xyz",
"DynamicLevel": "xyz",
"DynamicLevel": "xyz",
"dynamic": "xyz"
}]';
In second response this json will be like below
var res = '[
{
"DynamicCity": "xyz",
"DynamicCountry": "xyz",
"DynamicState": "xyz",
"DynamicRegion": "xyz",
"dynamic": "xyz"
},
{
"DynamicCity": "xyz",
"DynamicCountry": "xyz",
"DynamicState": "xyz",
"DynamicRegion": "xyz",
"dynamic": "xyz"
},
{
"DynamicCity": "xyz",
"DynamicCountry": "xyz",
"DynamicState": "xyz",
"DynamicRegion": "xyz",
"dynamic": "xyz"
}]';
this data need to render in material table I have tried like this
// properties of the class
displayedColumns: string[];//columns should be create dynamically
dataSource = res;
<table mat-table [dataSource]="dataSource | keyvalue" class="mat-elevation-z8">
<ng-container matColumnDef="key">
<th mat-header-cell *matHeaderCellDef> {{element.key}}</th>
<td mat-cell *matCellDef="let element"> {{element.value}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
I need output like below
Here, dynamic column means in each api call we get different data for this column names so it will be changed every api call so I am not able to bind it to material table. Please help me to solve this issue of binding json data to material table. reference URLs:-
