Angularjs groupBy + orderBy [英] Angularjs groupBy + orderBy
问题描述
我正在使用 angular-filter 中的 groupBy 根据对象的日期属性对对象数组进行分组.
<h3>{{ 天 |日期:mediumDate }}
产生以下结果:
2015 年 2 月 9 日2015 年 2 月 10 日2015 年 2 月 11 日2015 年 2 月 12 日
如何倒序从最近的日期开始?当我打印到控制台时,数组以我想要的顺序打印:
对象 {1423699200000:数组[1],1423612800000:数组[7],1423526400000:数组[11],1423440000000:数组[1]}
我还写了一个自定义过滤器来反转 groupBy 之后的顺序:
.filter("reverseOrder", function() {函数 sortNumber(a,b) {返回 parseInt(b) - parseInt(a);}返回函数(集合){var keys = Object.keys(collection).sort(sortNumber);var reveredCollection= {};var length=collection.length;angular.forEach(键,功能(键){reveredCollection[key] = collection[key];});返回尊敬的集合;}})
我是这样申请的:
<h3>{{ 天 |日期:mediumDate }}
最近遇到了同样的问题.groupBy
创建了一个对象,但是 orderBy
需要一个数组,所以有一点点时髦.我最终直接从他们的问题页面获得了答案,其中一位作者出色地解释了它,并附有代码示例,这些示例基本上是我复制/粘贴的.
https://github.com/a8m/angular-filter/issues/57#issuecomment-65041792
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>
Which produces the following:
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]
}
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;
}
})
Which I have applied like this:
<div ng-repeat="(day, dayEvents) in events | groupBy: 'date' | reverseOrder )">
<h3>{{ day | date: mediumDate }}</h3>
</div>
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屋!