I'm trying to create a table automatically using Angular and HTML. I take data from a mysql database using PHP, and Angular can see them thanks to JSON.
In admin.component.html file I create the table using *ngFor in this way:
<div fxFlex fxLayoutAlign = "center" style="margin-top: 50px;">
<table cellpadding="5px">
<thead>
<th>Nome</th>
<th>Tipologia</th>
<th>Ingredienti</th>
<th>Allergeni</th>
</thead>
<tbody>
<tr *ngFor="let element of piatto;">
<td>{{element.NomeP}}</td>
<td>{{element.Tipo}}</td>
<td *ngFor="let i of element.Ingredienti;" >{{i.NomeI}}</td>
<td *ngFor="let a of element.Allergeni;" >{{a.NomeA}}</td>
</tr>
</tbody>
</table>
</div>
and the output is:
I would like to display the table in this way:
<table cellpadding="20px">
<thead>
<th>Nome</th>
<th>Tipologia</th>
<th>Ingredienti</th>
<th>Allergeni</th>
</thead>
<tbody>
<tr>
<td>Tagliolini al salmone</td>
<td>Primo</td>
<td>Pasta, Salmone</td>
<td>Glutine, Pesce</td>
</tr>
<tr>
<td>Macedonia</td>
<td>Dessert</td>
<td>Mela, Banana</td>
<td>Frutta</td>
</tr>
</tbody>
</table>
How can I do?
Here there is "piatto" definition from piatto.models.ts:
export class Piatto{
CodP !: number;
NomeP !: string;
Tipo !: string;
Ingredienti !: Array<any>;
Allergeni !: Array<any>; }
And here there is what "piatto" contains:
(2) [{…}, {…}]
0:
Allergeni: Array(2)
0: {CodA: "1", NomeA: "Glutine"}
1: {CodA: "4", NomeA: "Pesce"}
length: 2
__proto__: Array(0)
CodP: "1"
Ingredienti: Array(2)
0: {CodI: "3", NomeI: "Pasta", CodA: "1"}
1: {CodI: "8", NomeI: "Salmone", CodA: "4"}
length: 2
__proto__: Array(0)
NomeP: "Tagliolini al salmone"
Tipo: "Primo"
__proto__: Object
1:
Allergeni: Array(1)
0: {CodA: "8", NomeA: "Frutta a guscio"}
length: 1
__proto__: Array(0)
CodP: "5"
Ingredienti: Array(2)
0: {CodI: "9", NomeI: "Mela", CodA: "8"}
1: {CodI: "10", NomeI: "Banana", CodA: null}
length: 2
__proto__: Array(0)
NomeP: "Macedonia"
Tipo: "Dessert"
__proto__: Object
length: 2
__proto__: Array(0)