Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngFor create 2 rows

Tags:

html

angular

Using Angular 2 ngFor I'm creating a table.

My problem is that my data array contains elements such that each element should create 2 consecutive rows in the table (Using different fields, The second row is collapsible with more data)

<tbody>     <tr *ngFor="let element of data; let i = index">        <th>...<th>        ...        <td>...<td>     </tr> </tbody> 

Problem is that tbody doesn't allow any attribute beside <tr>.

I'm looking for something like

<tbody>    <template *ngFor="let element of data; let i = index">        <tr>...</tr> //row 1        <tr>...</tr> //row 2    </template> </tbody> 
like image 524
royB Avatar asked Aug 23 '16 09:08

royB


1 Answers

That exists with slightly different syntax:

<template ngFor let-element [ngForOf]="data" let-i="index">    <tr>...</tr> //row 1    <tr>...</tr> //row 2 </template> 

or

<ng-container *ngFor="let element of data let i=index">    <tr>...</tr> //row 1    <tr>...</tr> //row 2 </ng-container> 

update for >= 4.0.0 <template> was changed to <ng-template>

<ng-template ngFor let-element [ngForOf]="data" let-i="index">    <tr>...</tr> //row 1    <tr>...</tr> //row 2 </ng-template> 
like image 70
Günter Zöchbauer Avatar answered Sep 19 '22 02:09

Günter Zöchbauer