Angular Material Table 字母数字排序行为

标签 angular sorting angular-material

我在 Angular Material Table 中遇到问题,虽然它在技术上是正确的,但我在考虑是否有其他解决方法。

假设我有 5 个代码,F1F2F5F9F10.

Angular Material Table 的升序排序是,

F1
F10
F2
F5
F9

但我期待它是

F1
F2
F5
F9
F10

我的html代码在这里

<table mat-table [dataSource]="model.financingPurposeList" class="mat-elevation-z8" width="100%">

   <ng-container matColumnDef="code">
       <th mat-header-cell *matHeaderCellDef mat-sort-header> Code </th>
       <td mat-cell *matCellDef="let financingPurpose"> {{financingPurpose.code}} </td>
   </ng-container>

   <ng-container matColumnDef="description">
       <th mat-header-cell *matHeaderCellDef> Description </th>
       <td mat-cell *matCellDef="let financingPurpose"> {{financingPurpose.description}} </td>
   </ng-container>

   <tr mat-header-row *matHeaderRowDef="['code', 'description']; sticky: true"></tr>
   <tr mat-row *matRowDef="let row; columns: ['code', 'description'];" (click)="model.selectedFinancingPurpose.toggle(row)"></tr>

</table>

有没有办法做到这一点?

相关链接:

Natural Sorting

Sorting column containing both numbers and strings

最佳答案

这不是解决此问题的最佳解决方案,但我为此创建了一个简短而有效的解决方法。使用数组sort谓词函数。

// Following the example to the question

financingPurposeList.sort(function(a, b){
   return a.code.length - b.code.length;
});

关于Angular Material Table 字母数字排序行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55016642/

相关文章:

javascript - Angular 4 : Redirecting user to last visited page in previous session on login

ios - 应用两次排序无法正常工作

bash - 使用 os.system 正确处理从 Python 转义的 shell

javascript - Angular 的 Http 请求服务抛出错误

Angular:两个输入,单向镜像

apache - 如何将 Angular 4 应用程序部署到 apache htdocs?

html - 顶行的边框底部隐藏了第二行的边框属性

sorting - Elasticsearch 按字母顺序排序,然后按数字排序

css - mat-expansion-panel 添加下拉打开问题

angular - 居中垫片组