gpt4 book ai didi

Angular Material Table 字母数字排序行为

转载 作者:太空狗 更新时间:2023-10-29 17:19:16 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com