Angularjs groupBy + orderBy [英] Angularjs groupBy + orderBy

查看:473
本文介绍了Angularjs groupBy + orderBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angular-filter 中的groupBy按日期属性对对象数组进行分组。

I am using groupBy from angular-filter to group an array of objects by their date property.

<div ng-repeat="(day, dayEvents) in events | groupBy: 'date' )">
 <h3>{{ day | date: mediumDate }}</h3>
</div>

产生以下内容:

Feb 9, 2015 
Feb 10, 2015 
Feb 11, 2015 
Feb 12, 2015 

如何从最近的日期开始撤销订单?
当我打印到控制台时,阵列打印出我想要的订单:

How can I reverse the order to start from the most recent date? When I print to the console the array is printed with the order I want:

  Object {
     1423699200000: Array[1],
     1423612800000: Array[7],
     1423526400000: Array[11],
     1423440000000: Array[1]
 }

我还写了一个自定义过滤器来反转groupBy之后的订单:

I also wrote a custom filter to reverse the order after the groupBy:

.filter("reverseOrder", function() {
        function sortNumber(a,b) {
            return  parseInt(b) - parseInt(a);
        }
        return function(collection) {
            var keys = Object.keys(collection).sort(sortNumber);
            var reveredCollection= {};
            var length=collection.length;
            angular.forEach(keys, function(key) {
                reveredCollection[key] = collection[key];
            });
           return reveredCollection;
        }
    })

我有哪个appl是这样的:

Which I have applied like this:

<div ng-repeat="(day, dayEvents) in events | groupBy: 'date' | reverseOrder )">
     <h3>{{ day | date: mediumDate }}</h3>
</div>


推荐答案

最近有同样的问题。 groupBy 创建一个对象,但是 orderBy 需要一个数组,所以有一点点的乐趣。我最终直接从他们的问题页面得到答案,其中一位作者做了很好的解释工作它包含了基本上为我复制/粘贴的代码示例。

Recently had the same issue. groupBy creates an object, but the orderBy needs an array, so there's a little bit of funkiness involved. I ended up getting the answer straight from their issues page where one of the authors does an excellent job of explaining it complete with code samples that was basically copy/paste for me.

https://github.com/a8m/angular-filter/issues/57#issuecomment-65041792

这篇关于Angularjs groupBy + orderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆