Coral Adar
Coral Adar

Reputation: 31

How to create dynamic table in html and angular

I need to present in Html a dynamic table that I don't know what is the template of each row: Some times it contains 2 columns, sometimes 4...

I need something like this:

    <div>
   <h1>Angular HTML Table Example</h1>
   <table>
      <thead>
         <tr>
            <th>#ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Address</th>
            <th>Action</th>
         </tr>
      </thead>
      <tbody>
         <tr *ngFor="let item of ItemsArray">
            <th>{{ item.id }}</th>
            <td>{{ item.name }}</td>
            <td>{{ item.email }}</td>
            <td>{{ item.phone}}</td>
            <td>{{ item.address }}</td>
           
         </tr>
      </tbody>
   </table>
</div

And instead of:

<tr *ngFor="let item of ItemsArray">
        <th>{{ item.id }}</th>
        <td>{{ item.name }}</td>
        <td>{{ item.email }}</td>
        <td>{{ item.phone}}</td>
        <td>{{ item.address }}</td></tr>

somethong like:

 <tr *ngFor="let item of ItemsArray">
<ngFor="let property of item.structure>  
            <td>{{ property }}</td>

       

Do you have any advice for me?

Thanks

Upvotes: 0

Views: 1124

Answers (2)

Coral Adar
Coral Adar

Reputation: 31

This is the solution:

<tbody class="f">
             <ng-container *ngFor="let message of this.allMessages | filter:term | paginate: config  ;let i = index"> 
            <tr >
                <td>{{ i+1 }}</td>
                    <ng-container *ngFor = "let j of this.messageIndex">
                        <td>{{message.data[0][j]}}</td>
                    </ng-container>
             </tr> 
             </ng-container> 
        </tbody>

Upvotes: 0

Sandeep Adiseshu
Sandeep Adiseshu

Reputation: 78

You can get keys of list item by using below code and render header column

Object.keys(list);

This is stackblitz code

You can see required code in Product component file. I hope it works for you scenario

Upvotes: 2

Related Questions