【问题标题】:Unable to bind to data in mat-table无法绑定到 mat-table 中的数据
【发布时间】:2019-03-19 09:29:55
【问题描述】:

问题

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

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

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

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


更新:

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


代码

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

  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;
}

【问题讨论】:

  • 根据错误你必须提供SourceData是一个数组但是你的数据类型是对象形式。
  • 请添加您的模板代码。
  • 我假设您将items 绑定到mat-table。如果是这样,可能是items.rows绑定
  • 添加表格代码
  • @Tachyon 答案已更新。

标签: angular angular-material


【解决方案1】:

我注意到,您不需要为 API 响应包装 MatTableDataSource&lt;&gt;,

  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
  }

在这个 stackblitz example 中,dataSource 变量包含原始数据。 MatTableDataSource&lt;&gt; 不需要类型

更新:

这里正在工作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>

【讨论】:

  • 方法getApiRecords只是返回一个item数组,所以是可迭代的
  • @Tachyon 答案已更新。我没有看到你的更新。
  • 不幸的是不起作用。仍然给我同样的错误
  • @Tachyon 看来您的定义需要更改,答案已更新。请确保您的 mat-table 在服务响应后加载。
  • 对不起 - 检查我的问题的更新。它现在显示正确的行数,但没有数据。
猜你喜欢
  • 2018-10-22
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多