簡體   English   中英

如何使用材料 ZD18B8624A0F5F726DDDA7B823Z65FC5 將嵌套的 json arrays 顯示到 html 表中

[英]How to display nested json arrays into html table with material angular

我正在嘗試將嵌套的 json 數組顯示到我的材料 angular 表中。 如果我的 json 沒有嵌套的 arrays,我的數據工作正常。

Json

{
"rows": [
    {
        "mst": {
            "field": "createDate",
            "value": "2017-06-02"
        },
        "gsc": {
            "field": "Account/Audit/Creation/Date",
            "value": "2017-06-02"
        }
    },
    {
        "mst": {
            "field": "startDate"
        },
        "gsc": {
            "field": "startDate"
        }
    },
    {
        "mst": {
            "field": "status",
            "value": "ACTIVE"
        },
        "gscs": [
            {
                "field": "Account/LineOfBusiness/Type~Status",
                "value": "C~A"
            },
            {
                "field": "Account/LineOfBusiness/Type~Status",
                "value": "I~A"
            }                
        ],
        "gscvalue": "Active"
    },
    {
        "mst": {
            "field": "statusDate"
        },
        "gsc": {
            "field": "statusDate"
        }
    },
    {
        "mst": {
            "field": "statusReason"
        },
        "gsc": {
            "field": "statusReason"
        }
    },
    {
        "mst": {
            "field": "deliveryMethod",
            "value": "PAPER"
        },
        "gscs": [
            {
                "field": "Electronic",
                "value": "N"
            },
            {
                "field": "ElectronicOutsourced",
                "value": "N"
            },
            {
                "field": "Hardcopy",
                "value": "Y"
            }
        ],
        "gscvalue": "Paper"
    },
    {
        "mst": {
            "field": "statementFormat",
            "value": "Standard"
        },
        "gsc": {
            "field": "?"
        }
    },
    {
        "mst": {
            "field": "statementLanguagePreference",
            "value": "Spanish"
        },
        "gsc": {
            "field": "Account/Language",
            "value": "S"
        },
        "gscvalue": "Spanish"
    },
    {
        "mst": {
            "field": "type",
            "value": "RES"
        },
        "gsc": {
            "field": "Account/Type",
            "value": "RES"
        }
    }
]
}

HTML

<div class="example-container mat-elevation-z8" *ngIf="rows?.length>0">
<table mat-table [dataSource]="dataSource">

<ng-container matColumnDef="mst Fields">
  <th mat-header-cell *matHeaderCellDef> mst Fields </th>
  <td mat-cell *matCellDef="let row" class="tablePadding"> {{row.mst.field}} </td>
</ng-container>

<ng-container matColumnDef="mst">
  <th mat-header-cell *matHeaderCellDef> mst value </th>
  <td mat-cell *matCellDef="let row"> {{row.mst.value}} </td>
</ng-container>

<ng-container matColumnDef="gsc Fields">
  <th mat-header-cell *matHeaderCellDef> gsc Fields </th>
  <td mat-cell *matCellDef="let row"> {{row.gsc.field}} </td>
</ng-container>

<ng-container matColumnDef="gsc">
  <th mat-header-cell *matHeaderCellDef> gsc value </th>
  <td mat-cell *matCellDef="let row"> {{row.gsc.value}} </td>           
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns; let row"></tr>

模態的.ts

export class ResultField {
field: string;
value: string;
valueList: string[];
}

export class ResultRow {
mst: ResultField;
gsc: ResultField;
gscs: ResultField[];
gscValue: String;
}

我當前的表將一直顯示數據,直到達到 mst.field{status}。 我收到一個錯誤

SystemComponent.html:29 ERROR TypeError: Cannot read property 'field' of undefined

我知道這個錯誤的出現是因為我的 html 中的當前邏輯。 我該如何解決這個問題?

因此,我不希望 gscs.field 在單獨的列中,我希望將這些值放入 gsc.field 列和 gsc.value 列中的 gscValue 。

試圖

<ng-container matColumnDef="gsc Fields">
  <th mat-header-cell *matHeaderCellDef> gsc Fields </th>
  <td mat-cell *matCellDef="let row" *ngIf="row.gsc.field"> {{row.gsc.field}} </td>
<td mat-cell *matCellDef="let row" *ngIf="row.gsc.field"> {{row.gscs.field}} </td>
</ng-container>

但是對於 angular 邏輯,標簽中只能有一個 * 指令。

我最接近的參考是需要在材料數據表中顯示嵌套 JSON Object但沒有可靠的答案。

嘗試 2

我嘗試使用更簡單的方法來打印我的數據。

<div>
  <h1>Testing</h1>

  <div *ngFor="let row of rows">
    <!-- <p *ngIf="row.gsc.field">{{row.gsc.field}}</p>
    <p *ngIf="row.gsc.value">{{row.gsc.value}}</p> -->
    <p>{{row.mst.field}}</p>
    <p>{{row.mst.value}}</p>   


  <div *ngFor= "let gscs of row.gscs">
    <p *ngIf="gscs.field">{{gscs.field}}</p>
    <p *ngIf="gscs.value">{{gscs.value}}</p>
</div>
<p *ngIf = "row.gscvalue">{{row.gscvalue}}</p>
  </div>  
</div>

使用此代碼片段,我可以提取嵌套值,但是當我取消注釋這兩行時,我得到與上面相同的錯誤。

<!-- <p *ngIf="row.gsc.field">{{row.gsc.field}}</p>
<p *ngIf="row.gsc.value">{{row.gsc.value}}</p> -->

錯誤類型錯誤:無法讀取未定義的屬性“字段”

使用上面提供的 JSON,我想創建一個如下圖所示的表。

最終輸出

好像你只需要嵌套你的ngIf

<ng-container matColumnDef="gsc Fields">
  <th mat-header-cell *matHeaderCellDef> gsc Fields </th>
  <td mat-cell *matCellDef="let row">
    <ng-container *ngIf="row.gsc?.field; else fieldArray">
      {{row.gsc.field}} 
    </ng-container>
    <ng-template #fieldArray>
      <div class="sub-cell" *ngFor="let field of row.gscs"> <!-- need the appropriate css -->
        {{field.field}} {{field.value}}
      </div>
    </ng-template>
  </td>
</ng-container>

在你的價值列中,你想要更多:

<ng-container matColumnDef="gsc">
  <th mat-header-cell *matHeaderCellDef> gsc value </th>
  <td mat-cell *matCellDef="let row"> {{row.gsc?.value || row.gscvalue}} </td>           
</ng-container>

不確定這是 100%,但應該接近

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM