gpt4 book ai didi

angular - 收到错误 : Could not find column with id "id"

转载 作者:太空狗 更新时间:2023-10-29 17:47:17 27 4
gpt4 key购买 nike

我试图用数据填充 mat-table 但我收到错误:Could not find column with id "id" when I try to do so.

这是我在组件文件中的做法:

    export class ListAllTrucksComponent {
displayedColumns: string[] = ['id', 'plate', 'status', 'type'];
orders: Order[] = [];
dataSource: MatTableDataSource<Order>;

constructor(private orderService: OrderService) {
orderService.getAll().subscribe((orders) => {
for (const order of orders) {
const newOrder = new Order(order[0], order[1], order[2], order[3]);
this.orders.push(newOrder);
}
console.log(this.orders);
this.dataSource = new MatTableDataSource<Order>(this.orders);
});
}

applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}

这是我基本上从示例中复制的 html:

    <main>
<div id="content">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>

<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.plate}} </td>
</ng-container>

<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.status}} </td>
</ng-container>

<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef> Symbol </th>
<td mat-cell *matCellDef="let element"> {{element.type}} </td>
</ng-container>

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

当我在 console.log orders 中返回时:

enter image description here

最佳答案

您对 matColumnDef="" 属性的定义是错误的。请参见下面的示例,

    <!-- Position Column -->
<ng-container matColumnDef="position"> <!--matColumnDef PROPERTY IS "position" HERE. -->
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td> <!--AND "position" IS ALSO USED HERE.-->
</ng-container>

所以你的代码需要像这样:

    <main>
<div id="content">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

<!-- Position Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>

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

关于angular - 收到错误 : Could not find column with id "id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53758215/

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