无法绑定到Mat表中的数据

时间:2019-03-19 09:29:56

标签: angular angular-material

问题

我正在尝试在Angular 7中实现mat-table,但它给了我以下错误:

  

错误错误:找不到其他支持对象'[对象对象]'   类型为“对象”。 NgFor仅支持绑定到Iterables,例如   数组。

该表确实可以正确拾取该表的列,只是不显示任何数据。我尝试将dataSource更改为我的项目的直接数组,而不是使用MatTableDataSource,但是我丢失了标题,并且仍然无法显示数据。

如果有人可以向我指出我做错了什么,我将不胜感激。


更新:

它现在显示数据中存在的行数,但没有标题或任何数据: Missing elements


代码

填充我的项目数组的代码如下:

  async ngOnInit() {
    // Load the items from the blockchain
    const items = await this.api.getApiRecords();
    this.items = new MatTableDataSource<ApiRecord>(items);
  }

表本身定义如下:

<mat-table [dataSource]="items" matSort>

  <ng-container matColumnDef="type">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Type </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.type}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="provider">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Provider </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.provider}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="url">
    <mat-header-cell *matHeaderCellDef mat-sort-header> URL </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.url}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="country">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Country </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.country}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="updated_on">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Updated </mat-header-cell>
    <mat-cell *matCellDef="let item"> {{item.updated_on}} </mat-cell>
  </ng-container>

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

<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>

我正在从我的API中获取以下数据:

{
    "rows": [
        {
            "id": 0,
            "type": "xxx",
            "provider": "xxx",
            "srvname": "xxx",
            "url": "xxx",
            "continent": "xxx",
            "country": "xxx",
            "flags": 0,
            "revision": 2,
            "updated_on": "2019-03-15T14:03:25",
            "audited_by": "",
            "audited_on": "1970-01-01T00:00:00",
            "audit_ipfs_file": ""
        }
    ],
    "more": false
}

然后我要序列化为定义如下的对象:

export class ApiResponse {
  rows: ApiRecord[];
  more: boolean;
}

ApiRecord如下:

export class ApiRecord {
  id: number;
  type: string;
  provider: string;
  srvname: string;
  url: string;
  continent: string;
  country: string;
  flags: number;
  revision: number;
  updated_on: Date;
  audited_by: string;
  audited_on: Date;
  audit_ipfs_file: string;
}

1 个答案:

答案 0 :(得分:1)

我注意到,您不需要为API响应包装MatTableDataSource<>

  async ngOnInit() {
    // Load the items from the blockchain
    this.items = await this.api.getApiRecords(); //this will be work
    //this.items = new MatTableDataSource<ApiRecord>(items); << remove this
  }

在此example堆栈闪电中,dataSource变量包含原始数据。不需要MatTableDataSource<>

的类型

更新:

这里在工作stackblitz example

下面的代码也将更新,

  <mat-header-row *matHeaderRowDef="['type', 'provider', 'url', 'country', 'updated_on']"></mat-header-row>
  <mat-row *matRowDef="let row; columns: ['type', 'provider', 'url', 'country', 'updated_on']">
  </mat-row>
相关问题