我正在尝试生成一个表格,以学习如何使用Angular Material提供的表格功能,但此刻我陷入了困境。我猜这只是我所忽略的非常基本的东西。希望你们中的一个向导看到我在做什么错(忽略模板中尚未实现的导航到另一个页面部分)。
我的控制台出现以下错误:
ERROR Error: Could not find column with id "id".
.ts文件:
import { Component, OnInit, ViewChild } from "@angular/core";
import { Router } from "@angular/router";
import { MatSort } from "@angular/material/sort";
import { MatTableDataSource } from "@angular/material/table";
import { metingen } from "../mocks/metingen.mock";
@Component({
selector: "app-overzicht-metingen",
template: `
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef mat-sort-header>No.</th>
<td mat-cell *matCellDef="let element">{{ element.id }}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let element">
{{ element.datumVanDeMeting }}
</td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Weight</th>
<td mat-cell *matCellDef="let element">{{ element.producttype }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
`,
styles: []
})
export class OverzichtMetingenComponent implements OnInit {
// toevoegen van een meting
displayedColumns: string[] = ["id", "datumVanDeMeting", "producttype"];
dataSource = new MatTableDataSource(metingen);
@ViewChild(MatSort, { static: true }) sort: MatSort;
constructor(private router: Router) {}
ngOnInit() {
this.dataSource.sort = this.sort;
}
onSelect(id: string) {
this.router.navigate(["/inzien-meting", id]);
}
}
带有模拟数据的文件:
import { Meting } from "../models/meting";
export const metingen: Meting[] = [
{
id: "1",
datumVanDeMeting: new Date(),
producttype: "variant A",
sensor1: [1, 2, 3],
sensor2: [2, 3, 4],
sensor3: [3, 4, 5],
sensor4: [4, 5, 6]
},
{
id: "2",
datumVanDeMeting: new Date(),
producttype: "variant B",
sensor1: [3, 2, 2],
sensor2: [4, 3, 3],
sensor3: [5, 4, 4],
sensor4: [6, 5, 5]
},
{
id: "3",
datumVanDeMeting: new Date(),
producttype: "variant A",
sensor1: [9, 2, 9],
sensor2: [8, 3, 8],
sensor3: [7, 4, 7],
sensor4: [6, 5, 6]
},
{
id: "4",
datumVanDeMeting: new Date(),
producttype: "variant B",
sensor1: [1, 4, 3],
sensor2: [2, 3, 7],
sensor3: [3, 9, 5],
sensor4: [8, 5, 6]
},
{
id: "5",
datumVanDeMeting: new Date(),
producttype: "variant A",
sensor1: [7, 3, 3],
sensor2: [2, 4, 7],
sensor3: [8, 5, 5],
sensor4: [4, 7, 9]
}
];