数字和文本列的席子排序问题 [英] Mat Sort Issue for Numbers and Text Columns
问题描述
我有角形材料数据源.角材版本为^ 5.0.3排序正常.但是,对于某些列,它的排序不正确.数字和文字在那里.例如,排序的结果如"XXX","1","1tesxt","1","OPD","OXD","12".
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>
感谢您的帮助.
推荐答案
这是因为标准sortingDataAccessor
将数字字符串转换为数字,而在Javascript 1 > 'one'
和1 < 'one'
中,两者均求值为false
.
This is because the standard sortingDataAccessor
casts number strings to number and in Javascript 1 > 'one'
and 1 < 'one'
both evaluates to false
.
作为解决方法,您可以定义自己的sortingDataAccessor
而无需强制转换:
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];
}
解决方法是从 Github问题中复制的.
The workaround is copied from this Github issue.
这篇关于数字和文本列的席子排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!