如何使用Material Angular将嵌套的JSON数组显示为HTML表格

4

我将尝试把一个嵌套的json数组展示到我的材料角表中。如果我的json没有嵌套数组,那么我的数据是正常的。

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>

modal.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列中,并将gscValue放入gsc.value列中。

尝试:

<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逻辑,一个标签中只能有一个*指令。

我最接近的参考资料是需要在Material Data Table中显示嵌套的JSON对象,但没有确定的答案。

尝试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> -->

错误类型错误:无法读取未定义的“field”属性

使用我提供的JSON,我想创建一个类似以下图片的表格。

Final Output


所以当有一个数组时,您希望有多个单元格来表示它? - bryan60
@bryan60 是的,没错。只有当我们无法在同一单元格中设置gscs.field和gscs.value时,它才能是gscs.field。 - Mike.Chun
1个回答

3

看起来你只需要嵌套你的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>

我不确定这是否完全正确,但应该很接近。


谢谢Bryan!你是救命恩人!这帮了很大的忙。计时器结束后会给你提供赏金。 - Mike.Chun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接