我正在使用 Angular 来渲染 mat-table 并且 mat-sort 不起作用。这是我的 HTML 代码(用于渲染单元格的 ng-template,但我很确定这不会影响 mat-sort):
<table mat-table [dataSource]="dataSource" matSort>
<ng-container
[matColumnDef]="column"
*ngFor="let column of displayedColumns"
>
<th
mat-header-cell
mat-sort-header
*matHeaderCellDef
>
<span>
{{ column }}
</span>
</th>
<td mat-cell *matCellDef="let element">
<ng-container
*ngTemplateOutlet="
renderCell;
context: { element: element, column: column }
"
></ng-container>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
这是我的 .ts:
@ViewChild(MatSort, { static: false })
public sort: MatSort;
constructor(
) {}
ngOnInit(): void {}
ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
}