I'm trying to create dinamically the elements of a table and I have a doubt.
How can I create new <tr>
for example each 4 <td>
and then continue creating new <td>
<table>
<tr>
<td class="category-card" *ngFor= "let category of categories">
<img class="product-card-image" src="{{category.icon}}">
<span class="barImage">{{category.title}}</span>
</td>
</table>
EDIT:
<table>
<ng-template *ngFor="let category of categories; let i = index">
<tr *ngIf="(i % 4) == 0">
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/{{category.icon}}.png">
<span class="barImage">{{category.titulo}}</span>
</td>
</tr>
</ng-template>
</table>
EDIT2: I've added
<table [outerHTML]="categorias | dynamicTablePipe"></table> on my html
This is a external class (I've declared on NGModule)
@Pipe({
name: 'dynamicTablePipe'
})
export class DynamicTablePipe implements PipeTransform{
transform(contents) {
let template = ``;
let index = 0;
for (let content of contents) {
let currentTemplate = ``;
if (index === 0) {
// check if first record
currentTemplate = `
<tr>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/${content.icono}.png">
<span class="barImage">${content.titulo}</span>
</td>`;
} else if ((index % 4) === 0) {
// check if multiple of 4
currentTemplate = `
</tr>
<tr>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/${content.icono}.png">
<span class="barImage">${content.titulo}</span>
</td>`;
} else {
// normal row
currentTemplate = `
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/${content.icono}.png">
<span class="barImage">${content.titulo}</span>
</td>`;
}
index++;
template += currentTemplate;
}
return template;
}
}
The code inside of the pipe works fine, this is the template string formed at the end:
<tr>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/smartphone.png">
<span class="barImage">Telefonia</span>
</td>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/gamepad.png">
<span class="barImage">Videojuegos</span>
</td>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/laptop.png">
<span class="barImage">Portatiles</span>
</td>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/speaker.png">
<span class="barImage">Reproductores</span>
</td>
</tr>
<tr>
<td class="product-card">
<img class="product-card-image" src="/app/assets/img/link.png">
<span class="barImage">Redes</span>
</td></tr>
The problem is in the moment to return
thanks