数字和文本列的垫子排序问题 [英] Mat Sort Issue for Numbers and Text Columns
问题描述
我有角材料数据源.角材料版本是 ^5.0.3 排序工作.但是,对于某些列,它的排序不正确.数字和文本在哪里.例如,排序结果如XXX"、1"、1tesxt"、1"、OPD"、OXD"、12".
感谢您的帮助.
这是因为标准的 sortingDataAccessor
将数字字符串转换为数字并且在 Javascript 中 1 >'一个'
和 1 <'one'
都计算为 false
.
作为一种解决方法,您可以定义自己的 sortingDataAccessor
而无需强制转换:
ngAfterViewInit() {this.dataSource.sort = this.sort;this.dataSource.sortingDataAccessor =(数据,属性)=>数据[属性];}
此解决方法复制自此 Github 问题.>
I have angular material datasource. angular material version is ^5.0.3 Sorting is working. However for some columns it is sorting incorrectly. where number and text is there. For instance, sorted result like, 'XXX', '1', '1tesxt', '1', 'OPD', OXD', '12'.
<mat-table #table [dataSource]="dataSource" matSort >
<ng-container matColumnDef="model">
<mat-header-cell *matHeaderCellDef mat-sort-header> Model </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.model}} </mat-cell>
</ng-container>
Appreciate your help.
This is because the standard sortingDataAccessor
casts number strings to number and in Javascript 1 > 'one'
and 1 < 'one'
both evaluates to false
.
As a workaround, you can define your own sortingDataAccessor
without the cast:
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.sortingDataAccessor = (data, attribute) => data[attribute];
}
The workaround is copied from this Github issue.
这篇关于数字和文本列的垫子排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!