【发布时间】:2018-10-03 09:50:51
【问题描述】:
我必须按字母顺序显示我的所有报告。
<ul class="reports-list" style="list-style-type:none;">
<ng-container *ngFor="let report of reports | sort">
<li id="report-number-{{report.id}}"><div id="report-button-{{report.id}}" class="btn report-name-button border-bottom standard-color" (click)="chooseReport(report)">{{ report?.name | translate }} </div>
</li>
</ng-container>
</ul>
我试图使用这个管道,但它没有按那个顺序显示:
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "sort"
})
export class ArraySortPipe {
transform(array: Array<string>, args: string): Array<string> {
array.sort((a: any, b: any) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return array;
}
}
有人可以帮帮我吗?问候。
【问题讨论】:
-
你能在这里发布你的虚拟 JSON 以供参考吗?
-
报告真的是一个字符串数组吗?因为你在你的模板中做“reports.id”我不这么认为