Tabela de dados de material angular com linhas dinâmicas
Estou usando a tabela de dados Angular 5 e Angular Material para a construção dos dados. Estou referindo um exemplo no site abaixo. Nesse aspecto, preciso incluir os dados dinâmicos em cada linha, como na captura de tela em que 'favorite' é o cabeçalho da coluna.
http://www.devglan.com/angular/angular-data-table-example
Exemplo de Json para referência cruzada.
constELEMENT_DATA: Element[
]=[
{
position: 1,
firstName: 'John',
lastName: 'Doe',
email: '[email protected]'favoriteColor: 'red',
favorite: {
favoriteFood: [
'Idly',
'Vada'
],
favoriteCar: 'Mercendes Benz',
favoriteMovie: 'JamesBond movie',
favoritePlace: 'HillStation'
}
},
{
position: 1,
firstName: 'Mike',
lastName: 'Hussey',
email: '[email protected]',
favorite: {
favoriteFood: 'Dosa',
favoriteMovie: 'RajiniKandth movie'
}
},
{
position: 1,
firstName: 'Ricky',
lastName: 'Hans',
email: '[email protected]',
favorite: {
favoriteFood: 'Chappathi',
favoriteCar: 'BMW'
}
},
{
position: 1,
firstName: 'Martin',
lastName: 'Kos',
email: '[email protected]'favorite: {
}
},
{
position: 1,
firstName: 'Tom',
lastName: 'Paisa',
email: '[email protected]',
favorite: {
favoriteCar: 'Ford'
}
}
];
Html:
<mat-table #table [dataSource]="dataSource">
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> </mat-cell>
</ng-container>
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef> First Name </mat-header-cell>
<mat-cell *matCellDef="let element"> </mat-cell>
</ng-container>
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef> Last Name </mat-header-cell>
<mat-cell *matCellDef="let element"> <mat-cell>
</ng-container>
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef> Email </mat-header-cell>
<mat-cell *matCellDef="let element"> </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Meu erro foi incapaz de capturar a saída corretamente na captura de tela. Se você considerar o primeiro nome John como primeira linha, Comida: Ociosamente, Vada aparecerá nessa linha, mas a próxima linha terá Carro: Mercendes Benz, a próxima linha terá o filme JamesBond e o próximo local: HillStation. onde todas as outras colunas pertencentes a essas linhas ficarão em branco. A próxima iteração será iniciada para o nome Ricky de maneira semelhante. No Json, considere que todos esses itens favoritos estão sob favorito.