要解决Angular 8中的Material Sort在DataTable(MatTable)中不起作用的问题,可以按照以下步骤进行:
import { MatSortModule } from '@angular/material/sort';
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponent implements OnInit {
@ViewChild(MatSort, {static: true}) sort: MatSort;
dataSource: MatTableDataSource;
// ...
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
// ...
}
Column 1
{{element.column1}}
通过以上步骤,你应该可以使Material Sort在DataTable(MatTable)中正常工作了。确保你已经正确设置了数据源(dataSource)和要排序的列定义(matColumnDef),以及在模板中引入了MatSortHeader组件。