Angular orderBy 对象可能吗? [英] Angular orderBy object possible?
问题描述
我有一个表示日历日期的 JSON 对象.这些是通过 CMS 添加的,我希望能够根据日期过滤它们.我的模式设置使这比我想象的更困难.是否可以在此 JSON 对象中按天排序,或者是否有过滤器解决方法?
这是我的 JSON 对象:
<代码>{"_id" : ObjectId("53f252537d343a9ad862866c"),年" : {十二月":[],十一月" : [],十月":[],九月" : [],八月" : [],七月" : [{天":21","title" : "事件标题","summary" : "事件概要","description": "o事件描述","_id" : ObjectId("53f252537d343a9ad862866d")}],六月" : [],可能" : [],四月" : [],行进" : [],二月":[],一月" : []},__v":0}
这是我已经使用自定义过滤器按月过滤的视图.orderBy 不起作用,但我将它留作占位符以显示我想在哪里设置功能.</p>
<div ng-repeat="cal in calendar[n].year | filterKey:month"><div ng-if="cal != '' "><div class="日历"><div ng-repeat="校准中的项目 | orderBy: 'key.day' "><a href="/events/{{item.day}}"><文章类=事件列表"><div class="numberedDate"><h3>{{item.day}}</h3><div class="calInfo"><h5>{{item.title}}</h5><p>{{item.summary}} <a>more</a></p>
</文章></div><!-- ng-repeat val,key --></div><!-- 日历--></div><!-- ng-if cal --></div><!-- ng-repeat cal --></div><!-- calDynamic -->
您应该能够定义一个自定义排序函数,该函数根据对象中的任何项目进行排序.关键是在过滤函数中将对象转换为数组.
举个例子:
app.filter('orderByDayNumber', function() {返回函数(项目,字段,反向){var 过滤 = [];angular.forEach(项目,功能(项目){过滤.推(项目);});过滤排序(函数(a,b){return (a[field] > b[field] ? 1 : -1);});如果(反向)过滤.反向();返回过滤;};});
然后你会这样称呼它:
注意,您不应该像假设的那样编写 val.day
.
查看这篇很棒的博文了解更多信息.
EDIT:事实上,看起来您的结构实际上已经是一个数组,因此虽然此技术仍然有效,但可能没有必要 - 它可能只是您添加的方式导致问题的 orderBy 参数.
I have a JSON object representing calendar dates. These are added through a CMS and I'd like to be able to filter them based on date. My schema set-up has made this more difficult than I thought. Is it possible to orderBy the day value in this JSON object or is there a filter workaround?
Here is my JSON object:
{
"_id" : ObjectId("53f252537d343a9ad862866c"),
"year" : {
"December" : [],
"November" : [],
"October" : [],
"September" : [],
"August" : [],
"July" : [
{
"day" : "21",
"title" : "Event Title",
"summary" : "Event Summary",
"description" : "oEvent Description",
"_id" : ObjectId("53f252537d343a9ad862866d")
}
],
"June" : [],
"May" : [],
"April" : [],
"March" : [],
"February" : [],
"January" : []
},
"__v" : 0
}
Here is my view which already uses a custom filter to filter by month. The orderBy is not functioning but I've left it in as a placeholder to show where I'd like to set the functionality.
<div class="calDynamic" data-ng-repeat="n in [] | range:100">
<div ng-repeat="cal in calendar[n].year | filterKey:month">
<div ng-if="cal != '' ">
<div class="calendar">
<div ng-repeat="item in cal | orderBy: 'key.day' ">
<a href="/events/{{item.day}}">
<article class="eventslist">
<div class="numberedDate">
<h3>{{item.day}}</h3>
</div>
<div class="calInfo">
<h5>{{item.title}}</h5>
<p>{{item.summary}} <a>more</a></p>
</div>
</article>
</div><!-- ng-repeat val,key -->
</div><!-- calendar -->
</div><!-- ng-if cal -->
</div><!-- ng-repeat cal -->
</div><!-- calDynamic -->
解决方案 You should be able to define a custom sort function that sorts by any item in your object. The key bit is to convert the object to an array in the filter function.
Here's an example:
app.filter('orderByDayNumber', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
You would then call it like this:
<div ng-repeat="(key, val) in cal | orderByDayNumber: 'day' ">
Note, you shouldn't write val.day
as that is assumed.
Look at this great blog post here for more info.
EDIT: In fact, it looks like your structure is actually already an array, so while this technique will still work, it may not be necessary - it might have just been the way you were adding the parameter to orderBy that was causing issues.
这篇关于Angular orderBy 对象可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文