1

I want to get table head data dynamically how i do that??

My HTML:-

<table border="1">
                <thead>
                    <tr *ngFor="let SR of StockReport;">
                        <td>
                            <b></b>
                        </td>
                        <td><b></b></td>
                        <td><b></b></td>
                        <td><b></b></td>
                        <td><b></b></td>
                      
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let SR of StockReport;let inx=index">
                        <td style="width: 11%;">{{inx+1}} </td>
                        <td>{{SR.ItemName}} </td>
                        <td align="right">{{SR.Inward}} </td>
                        <td align="right"> {{SR.Outward}}</td>
                        <td align="right">{{SR.Stock}} </td>

                    </tr>
                </tbody>
            </table>
In the above code, the columns are getting created successfully but the table head data is not working

json data:- { "ItemName": "CV CPU", "Inward": "50.00", "Outward": "0.00", "Stock": "50.00" },

2
  • can you create stackblitz ? Commented Jun 26, 2020 at 10:39
  • try my code given in answer. Commented Jun 26, 2020 at 10:43

1 Answer 1

1

Demo : https://stackblitz.com/edit/angular6-keyvaluepipe-demo-zcesna

<table border="1">
                <thead>
                    <tr>
                        <td> Sr. No.</td>
                        <td *ngFor="let key of tableHeader ">
                            <b>{{key}}</b>
                        </td>
                         
                      
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let SR of StockReport; let i = index">
                        <td style="width: 11%;">{{i+1}} </td>
                        <td>{{SR.BranchID}} </td>
                         <td align="right">{{SR.Brand}} </td>
                       
                        <td align="right">{{SR.Inward}} </td>
                        
                        <td align="right">{{SR.ItemID}} </td>
                         <td>{{SR.ItemName}} </td>
                         <td>{{SR.ItemSubGrp}} </td>
                         <td align="right">{{SR.ModelNO}} </td> 
                         
                          <td align="right"> {{SR.Outward}}</td>
                          <td align="right">{{SR.Stock}} </td>

                    </tr>
                </tbody>
            </table>
Sign up to request clarification or add additional context in comments.

10 Comments

thats not working there appear key index only not key names
it's works bt i want it directly through api response
whatever API response you are getting JSON map it to variable then use in template. This is approach you have to use. I given sample JSON in component. If you feel this works dont forget to accept and upvote answer. Thanks
@sHaRjAs you got my points ? you have to handle in API call.
How i randomly select these keys and keyvalues
|

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.