在处理多级排序时,需要使用matSort封装一个对象来跟踪排序方式。以下示例显示如何使用matSort对象在多个表格之间同步排序。
HTML模板代码:
ID | {{item.id}} | Name | {{item.name}} |
---|
Age | {{item.age}} | Address | {{item.address}} |
---|
TypeScript代码:
import { ViewChild } from '@angular/core'; import { MatSort } from '@angular/material/sort';
export class YourComponent { @ViewChild(MatSort, { static: true }) sort1: MatSort; @ViewChild(MatSort, { static: true }) sort2: MatSort;
// dataSource1 数据源定义,省略
// dataSource2 数据源定义,省略
// 在 ngOnInit 生命周期里启用排序
ngOnInit() {
this.dataSource1.sort = this.sort1;
this.dataSource2.sort = this.sort2;
}
}
在此示例中,我们使用@ViewChild标识符获取了两个matSort对象sort1和sort2。我们在ngOnInit
上一篇:angular8的路由守卫`canActivate`不起作用
下一篇:Angular8的ng-number-picker组件 - 在`mousedown`事件上保持计数的增加,并移开旋转按钮。