简体   繁体   English

如何以角度显示嵌套json数组中的表?

[英]How to display the table from nested json array in angular?

I'm using angular 7 and I want to display the nested json datas in the table. 我正在使用角度7,我想在表格中显示嵌套的json数据。 My Json structure: 我的Json结构:

{
  "id": "1",
  "referenceNumber": "P123",
  "supplierNote": "My Notes",
  "internalNote": "Your notes",
  "purchaseOrderProducts": [
   {
      "purchaseId": "p1",
      "quantity": 1000,
   },
   {
      "purchaseId": "p2",
      "quantity": 500,
   }
 ]
}

I want to display the value " purchaseId " and " quantity " in the table using angular. 我想使用angular在表格中显示值“ purchaseId ”和“ quantity ”。

Use *ngFor in the html for the above
assuming the above json is an Object purchase

<table>
<div *ngFor="let obj of purchase.purchaseOrderProducts">
<tr>{{obj.purchaseId}}</tr>
<tr>{{obj.quantity}}</tr>
</div>
</table>

If you want to display only purchaseid and quantity information then you can use *ngFor directive to loop over the purchaseOrderProducts array and display the data. 如果您只想显示purchaseidquantity信息,那么您可以使用*ngFor指令循环遍历purchaseOrderProducts数组并显示数据。

HTML will look like below : HTML将如下所示:

<table bordar="2">
    <tr>
        <th>purchaseId</th>
        <th>quantity</th>
    </tr>
    <tr *ngFor="let data of arrObj?.purchaseOrderProducts">
        <td>    {{data.purchaseId}}</td>
        <td>{{data.quantity}}</td>
    </tr>
</table>

Here is the working example : 这是工作示例:

https://stackblitz.com/edit/angular-dfsfpr https://stackblitz.com/edit/angular-dfsfpr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM