how to componentize each row of an html table in angular

936 views
Skip to first unread message

rj

unread,
Apr 10, 2019, 6:11:43 PM4/10/19
to Angular and AngularJS discussion

I am new to Angular. I want to render each row of a html table as separate component using ngFor. I tried an approach but failed.


StackBlitz link - https://stackblitz.com/edit/angular-yaie9e?file=src%2Fapp%2Fapp.component.html


More Details below -

venues-list will be the parent table component. venue-item will be the component representing each row of a table.

I have implemented both the components with the required data and tried testing them. But i am getting bad results.


HTML snip of venues-list component -

<table class="table table-hover" [mfData]="filteredVenues" #mf="mfDataTable">
   
<thead>
     
<tr>
       
<th>Venue Name</th>
       
<th>User Name</th>
       
<th>HeartBeat</th>
     
</tr>
   
</thead>
   
<tbody>
     
<tr style="cursor: pointer;"
          *
ngFor="let venue of mf.data | sortgrid: '-lastheartbeat';"
          [
routerLink]="[venue.venueKey.id]"
         
routerLinkActive="active">
       
<app-venue-item></app-venue-item>
     
</tr>
   
</tbody>
</table>


HTML snip of venue-item component -


<td>
 
<span>Venue Name comes here</span>
</td>
<td>
 
<span>User Name comes here</span>
</td>
<td>
 
<span>HeartBeat comes here</span>
</td>


Data in latter HTML's td elements is a sample i tested before i load the actual data.


I believe <app-venue-item> selector will wrap the html of venue-item and will be rendered within venues-list component.


However, when i test this flow i see all the latter HTML's td elements loaded under the first thelement of former HTML.

What am i doing wrong or missing here?


Stéphane Ancelot

unread,
Apr 12, 2019, 4:51:13 AM4/12/19
to Angular and AngularJS discussion

 <tr app-venue-item *ngFor="let venue of mf.data | sortgrid: '-lastheartbeat';".........>              
               </tr>  

and declare the selector of app-venue-item as :
selector: 'tr[app-venue-item]',

Sander Elias

unread,
Apr 16, 2019, 12:51:44 AM4/16/19
to Angular and AngularJS discussion
Hi Rj,

Aside from what Stéphane suggested, you could also use `ng-container`

Regards
Sander
Reply all
Reply to author
Forward
0 new messages