如何从 MatSort 获取所有可排序列
How to get all sortable columns from MatSort
我有数据table,我想从后端应用排序过程,所以最终用户可以对多个列应用排序,我需要做的是获取所有columns
及其在每个 sortChange
事件上的排序值 asc/desc
。
假设这些列是:
name, gender, date_of_birth.
结果应该是:
[
{'name', 'asc'},
{'gender', 'desc'},
{'date_of_birth', 'desc'},
]
ts代码:
ngOnInit() {
this.getData([])
this.sort.sortChange.subscribe(
() => {
// access all the sortable columns
let sortable_columns = -------------- // what should I write here?
// call the http service
this.getData(sortable_columns)
});
}
现在我认为通用算法没问题,但我找不到访问 sortable 列的正确语法,请问如何操作?
谢谢。
我通过将活动列(最近排序的 MatSortable 列)存储在每个 SortCahnge
之后的单独结构中来解决这个问题。
HTML 脚本:
<mat-table matSort #table [dataSource]="dataSource" (matSortChange)="sortData($event)">
</mat-table>
ts代码:
ngOnInit() {
this.filterationArray['sort'] = [];
}
sortData(sort: Sort) {
let index = this.filterationArray['sort'].findIndex(
(elem) => {
return elem[0] == sort.active;
}
);
if (index == -1)
this.filterationArray['sort'].push([sort.active, sort.direction]);
else
this.filterationArray['sort'][index][1] = sort.direction;
console.log(this.filterationArray);
// call the get service
this.getDataFromBackEnd(this.filterationArray);
}
我有数据table,我想从后端应用排序过程,所以最终用户可以对多个列应用排序,我需要做的是获取所有columns
及其在每个 sortChange
事件上的排序值 asc/desc
。
假设这些列是:
name, gender, date_of_birth.
结果应该是:
[
{'name', 'asc'},
{'gender', 'desc'},
{'date_of_birth', 'desc'},
]
ts代码:
ngOnInit() {
this.getData([])
this.sort.sortChange.subscribe(
() => {
// access all the sortable columns
let sortable_columns = -------------- // what should I write here?
// call the http service
this.getData(sortable_columns)
});
}
现在我认为通用算法没问题,但我找不到访问 sortable 列的正确语法,请问如何操作? 谢谢。
我通过将活动列(最近排序的 MatSortable 列)存储在每个 SortCahnge
之后的单独结构中来解决这个问题。
HTML 脚本:
<mat-table matSort #table [dataSource]="dataSource" (matSortChange)="sortData($event)">
</mat-table>
ts代码:
ngOnInit() {
this.filterationArray['sort'] = [];
}
sortData(sort: Sort) {
let index = this.filterationArray['sort'].findIndex(
(elem) => {
return elem[0] == sort.active;
}
);
if (index == -1)
this.filterationArray['sort'].push([sort.active, sort.direction]);
else
this.filterationArray['sort'][index][1] = sort.direction;
console.log(this.filterationArray);
// call the get service
this.getDataFromBackEnd(this.filterationArray);
}