1

I am working with a table, where i require to apply the data visibility only available with header contains throw dynamic. how to achieve this?

here is my example:

columns = ['name', 'age']; //required fields declared. on change I need to update the data as well

html:

<table>
  <tr>
    <th *ngFor="let col of columns">{{col}}</th>
  </tr>
  <tr *ngFor="let item of data"> 
    <td >{{item.name}}</td> //how to apply from header instead of manual
    <td >{{item.age}}</td> //how to apply from header instead of manual
  </tr>
</table>

expecting like :

<tr *ngFor="let col of columns"> 
        <td >{{data('col')}</td> //name value  
        <td >{{data('col')}</td> //age value  
      </tr>
    </table>

Live Demo

1 Answer 1

1

You can loop through the columns too for td. And then on each item, you can access the property for the Object like item[column]

Give this a try:

<table>
  <tr>
    <th *ngFor="let col of columns; let i = index">{{setHeader(col, i)}}</th>
  </tr>
  <tr *ngFor="let item of data">
    <td *ngFor="let column of columns">{{item[column]}}</td>
  </tr>
</table>
Sign up to request clarification or add additional context in comments.

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.