要使Angular 6的mat-table可以排序,您需要执行以下步骤:
import { MatSortModule } from '@angular/material/sort';
@NgModule({
...
imports: [
...
MatSortModule
],
...
})
export class YourModule { }
import { MatSort, MatSortable } from '@angular/material/sort';
@ViewChild(MatSort, {static: true}) sort: MatSort;
ngOnInit() {
this.dataSource.sort = this.sort;
}
sortData(sort: MatSortable) {
// 根据您的需求实现排序逻辑
}
Column 1
{{element.column1}}
...
export class YourComponent implements OnInit, MatSortable {
dataSource: MatTableDataSource;
...
sortData(sort: MatSortable) {
const data = this.dataSource.data.slice();
if (!sort.active || sort.direction === '') {
this.dataSource.data = data;
return;
}
this.dataSource.data = data.sort((a, b) => {
const isAsc = sort.direction === 'asc';
// 根据您的需求实现排序逻辑
return 0;
});
}
}
请注意,上述代码中的排序逻辑是示例代码,您需要根据自己的数据结构和排序需求进行调整。
希望这可以帮助您解决Angular 6的mat-table无法排序的问题!
上一篇:Angular 6的Mat-Select通过http GET请求不定义user.email。
下一篇:Angular 6的MatPaginator在表格中不显示mat-option,并且没有设置下一页按钮和返回按钮的样式。