【发布时间】:2021-09-30 02:59:03
【问题描述】:
我在下面的 Angular 项目中创建了这个排序管道。结果是 我需要对“Accos, David pt., DIILL”进行排序,大写在这里似乎有问题。
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderBy',
})
export class OrderByPipe implements PipeTransform {
transform(array: any[], field: string): any[] {
if (!array) return array;
array = [...array];
array.sort((a: any, b: any) => {
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
} else {
return 0;
}
});
return array;
}
}
<ng-select
[items]="accounts$ | async | orderBy: 'businessName'"
[(ngModel)]="AccownerId"
bindLabel="businessName"
bindValue="id"
[searchable]="false"
id="Accowner"
>
</ng-select>
【问题讨论】:
-
它适用于更新版本...................................... ............................. 导出类 OrderByPipe 实现 PipeTransform { transform(array: any [], field: any): any[] { if (!array) 返回数组;数组 = [...数组]; array.sort((a, b) => a[field].toLowerCase().localeCompare(b[field].toLowerCase()));返回数组; } }