0

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

1
  • The *ngFor should be inside of the td tag so that it creates a cell for each column. Furthermore you should iterate over a Object.keys(item) so you can do *ngFor="let key of keys" ... item[key]. Commented Jul 14, 2020 at 6:33

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Should I write it in typescript? or HTML?
Just change the html code to this, <tr *ngFor="let item of ItemsArray"> <ngFor="let property of Object.keys(item)> <td>{{ item[property] }}</td>
you should write Objecy.keys(list) in typescript. Have a look at stackblitz.com/edit/…
The object looks like json without keys- somthing like that :["12", "firstItem"]
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.