我将尝试把一个嵌套的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,我想创建一个类似以下图片的表格。