angular2 pipe orderby(二)

前端之家收集整理的这篇文章主要介绍了angular2 pipe orderby(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import {Pipe,PipeTransform} from "@angular/core";

@Pipe({name: 'orderBy',pure: false})
export class OrderBy implements PipeTransform {

    value:string[] =[];

    static _orderByComparator(a:any,b:any):number {

        if(a === null || typeof a === 'undefined') a = 0;
        if(b === null || typeof b === 'undefined') b = 0;

        if((isNaN(parseFloat(a)) || !isFinite(a)) || (isNaN(parseFloat(b)) || !isFinite(b))) {
            //Isn't a number so lowercase the string to properly compare
            if(a.toLowerCase() < b.toLowerCase()) return -1;
            if(a.toLowerCase() > b.toLowerCase()) return 1;
        } else {
            //Parse strings as numbers to compare properly
            if(parseFloat(a) < parseFloat(b)) return -1;
            if(parseFloat(a) > parseFloat(b)) return 1;
        }

        return 0; //equal each other
    }

    transform(input:any,config:string = '+'): any {

        //make a copy of the input's reference
        this.value = [...input];
        var value = this.value;

        if(!Array.isArray(value)) return value;

        if(!Array.isArray(config) || (Array.isArray(config) && config.length == 1)) {
            var propertytocheck:string = !Array.isArray(config) ? config : config[0];
            var desc = propertytocheck.substr(0,1) == '-';

            //Basic array
            if(!propertytocheck || propertytocheck == '-' || propertytocheck == '+') {
                return !desc ? value.sort() : value.sort().reverse();
            } else {
                var property:string = propertytocheck.substr(0,1) == '+' || propertytocheck.substr(0,1) == '-'
                    ? propertytocheck.substr(1)
                    : propertytocheck;

                return value.sort(function(a:any,b:any) {
                    return !desc
                        ? OrderBy._orderByComparator(a[property],b[property])
                        : -OrderBy._orderByComparator(a[property],b[property]);
                });
            }
        } else {
            //Loop over property of the array in order and sort
            return value.sort(function(a:any,b:any) {
                for(var i:number = 0; i < config.length; i++) {
                    var desc = config[i].substr(0,1) == '-';
                    var property = config[i].substr(0,1) == '+' || config[i].substr(0,1) == '-'
                        ? config[i].substr(1)
                        : config[i];

                    var comparison = !desc
                        ? OrderBy._orderByComparator(a[property],b[property]);

                    //Don't return 0 yet in case of needing to sort by next property
                    if(comparison != 0) return comparison;
                }
                return 0; //equal each other
            });
        }
    }
}
原文链接:https://www.f2er.com/angularjs/148641.html

猜你在找的Angularjs相关文章