在Angular的MatSort中,可以通过自定义排序函数来更改默认的升序方法,以忽略0。以下是一个示例解决方法:
import { MatSort, MatSortable, MatTableDataSource } from '@angular/material';
dataSource: MatTableDataSource;
constructor() {
this.dataSource = new MatTableDataSource([]);
this.dataSource.sort = new MatSort();
}
ngOnInit() {
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'propertyName': // 替换为实际的属性名
return item.propertyName === 0 ? '' : item.propertyName;
default:
return item[property];
}
};
}
Property Name
{{element.propertyName}}
这样,当用户点击表头的排序按钮时,会调用排序函数来进行排序,忽略值为0的情况。