我有一個要求,我需要在每次按升序排列時對錶格的每一列進行排序。應用的邏輯是用於在Javascript中排序的一般邏輯。在所有情況下都能正常工作,除非數據與列中的數字不同。對角度爲2的html表格進行排序
的代碼是
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderBy'
})
export class OrderByPipe implements PipeTransform {
transform(records: Array<any>, args?: any): any {
return records.sort(function(a, b){
if(a[args.property] === null){
return 1 * args.direction;
}
else if(b[args.property] === null){
return -1 * args.direction;
}
else if(a[args.property] < b[args.property]){
return -1 * args.direction;
}
else if(a[args.property] > b[args.property]){
return 1 * args.direction;
}
else{
return 0;
}
});
};
}
當我像844401,76574893632,717613數據上面的代碼失敗,6304420005555
它上面,雖然它應該844401之前排序76574893632列出的順序排序值
作爲一個方面,角度團隊不鼓勵使用管道進行過濾或排序。你可以閱讀更多[這裏](https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe)。 – BogdanC
那麼你怎麼排序 – DP3
在你的組件。根據您的需要,您可以從服務中接收數組並對其進行排序,或者如果它已經顯示,並且您希望點擊命令,則可以將該數組排序,然後重新排序。 – BogdanC