【发布时间】: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 答案已更新。