我正尝试在mat表上实现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();
}正如您在顶部的屏幕截图中所看到的,排序属性是未定义的。我是否需要编写一些额外的代码来初始化该属性?
发布于 2019-11-19 19:08:50
您的MatTable是否封装在任何ngIf中?如果为true,你必须在ngAfterViewInit中设置你的排序,因为你的表没有被呈现,你的'MatSort‘不存在,或者你可以添加一个设置器,当你的MatSort存在的时候设置它。
@ViewChild('sort1', { static: false})set matSort(value: MatSort){
if(this.dataSource && value) {
this.dataSource.sort = value;
}
}https://stackoverflow.com/questions/58932037
复制相似问题