角度过滤器组与orderby

时间:2015-11-20 18:42:14

标签: angularjs angularjs-ng-repeat angular-filters

我试图按组值(日期和时间)排序groupBy列表。我正在使用angular-filter library

这是数据

[
  {
    "id": 1,
    "pickupDate": "2015-12-04",
    "time": "17:00"
  },
  {
    "id": 1,
    "pickupDate": "2015-12-03",
    "time": "12:00"
  },
  {
    "id": 2,
    "pickupDate": "2015-12-01",
    "time": "12:00"
  },
  {
    "id": 2,
    "pickupDate": "2015-12-02",
    "time": "09:00"
  },
  {
    "id": 3,
    "pickupDate": "2016-03-01",
    "time": "10:00"
  },
  {
    "id": 3,
    "pickupDate": "2015-12-01",
    "time": "11:00"
  },
  {
    "id": 4,
    "pickupDate": "2015-12-13",
    "time": "14:00"
  },
  {
    "id": 4,
    "pickupDate": "2015-12-02",
    "time": "11:15"
  }
]

我想做的事情:

  1. 按ID属性对数据进行分组(由| groupBy:' id'完成)
  2. 按照组中最小的日期时间对组进行排序
  3. 我在订购方面苦苦挣扎。

    此处列表应如何显示

    3
        01-12-2015 - 11:00
        01-03-2016 - 10:00
    
    2
        01-12-2015 - 12:00
        02-12-2015 - 09:00
    
    4
        02-12-2015 - 11:15
        13-12-2015 - 14:00  
    
    1
        03-12-2015 - 12:00
        04-12-2015 - 17:00
    

    这里是plunker link

    非常感谢!

1 个答案:

答案 0 :(得分:0)

我最终编写了自己的过滤器。我使用了lib下划线。

这是我在打字稿中的解决方案。

module App {

    export interface IFooFilter extends ng.IFilterService {
        (name:"foo-filter"): (collection:any[]) => any[];
    }

    class FooFilter {
        static $inject:string[] = ["underscore"];

        static ConvertDateAndTime(item:any):number {
            return item.pickupDate.replace(/\-/g, '') + item.time.replace(':', '');
        }

        public static Factory(underscore:UnderscoreStatic) {
            return underscore.memoize((collection:any[]) => {
                var groupKey = "id";

                var group:any = underscore.groupBy(collection, (item:any) => {
                    return item[groupKey];
                });

                var grpArray = [];
                angular.forEach(group, (item) => {
                    grpArray.push({
                        "groupKey": item[0][groupKey],
                        "items": item
                    });
                });

                var grpArraySorted = underscore.sortBy(grpArray, (grpObj:any) => {

                    var min:any = underscore.min(grpObj.items, (item:any) => {
                        return FooFilter.ConvertDateAndTime(item);
                    });

                    return FooFilter.ConvertDateAndTime(min);
                });

                return grpArraySorted;
            });
        }
    }

    angular.module("app").filter("fooFilter", FooFilter.Factory);
}

here the link to plucker