gpt4 book ai didi

html - 如何使用 Material Angular 将嵌套的json数组显示到html表中

转载 作者:行者123 更新时间:2023-12-03 19:14:50 31 4
gpt4 key购买 nike

我正在尝试将嵌套的 json 数组显示到我的 Material Angular 表中。如果我的 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 列和 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 逻辑,一个标签中只能有一个 * 指令。

我最接近的引用是 Need to display Nested JSON Object in Material Data Table
但没有可靠的答案。

尝试 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,我想创建一个如下图所示的表。

Final Output

最佳答案

似乎您只需要嵌套您的 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>

在您的值(value)列中,您想要更像:
<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% 但应该接近

关于html - 如何使用 Material Angular 将嵌套的json数组显示到html表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61147834/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com