【问题标题】:Need to display Nested JSON Object in Material Data Table需要在材料数据表中显示嵌套的 JSON 对象
【发布时间】:2019-08-25 22:38:31
【问题描述】:

我需要将来自后端的嵌套 JSON 对象显示为 MatTableDataSource 的列字段。

这是我的 JSON 对象:

[{
    "workstationId": 100,
    "assemblylineId": 100,
    "workstationDescription": "Testing1",
    "workstationTest": "Yes",
    "createdAt": "2019-03-20",
    "updatedAt": "2019-03-20",
    "assemblylines": [{
      "assemblylineName": "assembly1"
    }]
  },
  {
    "workstationId": 101,
    "assemblylineId": 100,
    "workstationDescription": "workstation1",
    "workstationTest": "No",
    "createdAt": "2019-04-04",
    "updatedAt": "2019-04-04",
    "assemblylines": [{
      "assemblylineName": "assembly5"
    }]
  },
  {
    "workstationId": 102,
    "assemblylineId": 101,
    "workstationDescription": "workstation2",
    "workstationTest": "No",
    "createdAt": "2019-04-04",
    "updatedAt": "2019-04-04",
    "assemblylines": [{
      "assemblylineName": "assembly4"
    }]
  },
  {
    "workstationId": 103,
    "assemblylineId": 102,
    "workstationDescription": "Testing2",
    "workstationTest": "Yes",
    "createdAt": "2019-04-04",
    "updatedAt": "2019-04-04",
    "assemblylines": [{
      "assemblylineName": "assembly3"
    }]
  }
]

这是我的用户界面: MatTableDataSource

这是我的工作站.model.ts

export interface Workstation {
  workstationId: number;
  workstationDescription: string;
  workstationTest: string;
  assemblylines: {
    assemblylineName: string;
  };
}

我查看了 JSON 对象解构、解析、字符串化的教程,但我没有到达那里,因为服务返回的是 Workstation[] 对象而不是 Workstation 对象。请让我知道是否有一种方法可以将 assemblylineName 属性显示为带有其值的列。

【问题讨论】:

  • 你能在ui中显示你想以哪种格式显示你的显示行吗?只是粗略的模型,以便我可以相应地为您提供帮助
  • 我想显示我的工作站所属的程序集的名称。数据作为嵌套 JSON 对象获取,我需要显示第二级:assemblylineName 属性作为第一级。 @FahadHassanSubzwari
  • 那么,如果会有多个装配线,那么您想在单个列中显示所有装配线名称(td)吗?
  • 不,JSON 对象是在我调用两个表的连接之后返回的。工作站只能属于一条装配线。
  • 有什么解决办法吗?我也有问题。

标签: node.js angular express angular-material sequelize.js


【解决方案1】:

你可以这样做:

.html

         <mat-table #table [dataSource]="workstationDataSource">

                // SR NUMBER
                <ng-container matColumnDef="sr_no">
                  <mat-header-cell *matHeaderCellDef>Sr. No.</mat-header-cell>
                  <mat-cell *matCellDef="let row">
                    <span>{{ row.workstationId }}</span>
                  </mat-cell>
                </ng-container>

                // DESCRIPTION
                <ng-container matColumnDef="description">
                  <mat-header-cell *matHeaderCellDef>Description</mat-header-cell>
                  <mat-cell *matCellDef="let row">
                    <span>{{ row.workstationDescription}}</span>
                  </mat-cell>
                </ng-container>

                // TEST
                <ng-container matColumnDef="test">
                  <mat-header-cell *matHeaderCellDef>Test</mat-header-cell>
                  <mat-cell *matCellDef="let row">
                    <span>{{ row.workstationTest}}</span>
                  </mat-cell>
                </ng-container>

                // ASSEMBLY LINES
                <ng-container matColumnDef="assembly_lines">
                  <mat-header-cell *matHeaderCellDef>Assembly Line</mat-header-cell>
                  <mat-cell *matCellDef="let row">
                    <span>{{ row.assemblylines.assemblylineName }}</span>
                  </mat-cell>
                </ng-container>

                // ACTIONS
                <ng-container matColumnDef="actions">
                  <mat-header-cell *matHeaderCellDef></mat-header-cell>
                  <mat-cell *matCellDef="let row">
                    <button (click)="edit(row)">EDIT</button>
                    <button (click)="delete(row)">DELETE</button>                    
                  </mat-cell>
                </ng-container>

                <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>

          </mat-table>

.ts

...
displayedColumns = [
  'sr_no',
  'description',
  'test',
  'assembly_lines',
  'actions'
];
workstationDataSource: MatTableDataSource<Workstation>;
...

// then you will need to populate the 'workstationDataSource' variable
getWorkstations() {
  this.http.get(...).subscribe(res => {
    ...
    this.workstationDataSource.data = new MatTableDataSource<Workstation>();
    this.workstationDataSource.data = res;
    ...
  });
}
...

希望对您有所帮助。

【讨论】:

  • 谢谢@Csaba,但我确实尝试过这种方法。它显示空白数据。
  • 不客气。控制台中有任何错误消息吗?
  • 没有。我没有在方括号中提到 assemblylineName 的索引。再次感谢您。
猜你喜欢
  • 2020-12-04
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
相关资源
最近更新 更多