gpt4 book ai didi

angular - 无法绑定(bind)到垫表中的数据

转载 作者:行者123 更新时间:2023-12-04 17:39:21 25 4
gpt4 key购买 nike

问题

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

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

然而,该表确实正确地选取了表的列,只是不显示任何数据。我已经尝试将 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;
}

最佳答案

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

  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<> 不需要类型

更新:

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

关于angular - 无法绑定(bind)到垫表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55237583/

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