我正在尝试在 mat-table 上实现 mat-sort,但是在尝试初始化 mat 排序功能时遇到了一些问题。排序属性未定义:
垫桌:
<mat-table matSort #sort1="matSort" matSortActive="id" matSortDirection="asc" matSortDisableClear
#table
[dataSource]="dataSource">
<ng-container matColumnDef="statusName">
<mat-header-cell *matHeaderCellDef mat-sort-header="customer.statusName">Status Name</mat-header-cell>
<mat-cell *matCellDef="let customer">
{{customer.statusName}}
</mat-cell>
</ng-container>
</mat-table>
组件代码:
@ViewChild('sort1', { static: true }) sort: MatSort;
ngOnInit(): void {
const sortSubscription = this.sort.sortChange.subscribe(() => (this.paginator.pageIndex = 0));
this.subscriptions.push(sortSubscription);
this.dataSource = new InvoiceStatusDataSource(this.invoiceStatusService);
this.loadItems(true);
}
在 loadItems 我有这个:
loadItems(firstLoad: boolean = false) {
const queryParams = new QueryParamsModel(
{},
this.sort.direction,
this.sort.active,
this.paginator.pageIndex,
this.paginator.pageSize
);
this.dataSource.loadItems(queryParams);
this.selection.clear();
}
正如您在顶部的屏幕截图中看到的那样,排序属性是未定义的。我是否需要编写一些额外的代码来初始化该属性?