角排序依据对象可能吗? [英] Angular orderBy object possible?

查看:172
本文介绍了角排序依据对象可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象重新presenting日历日期。这些都是通过一个CMS添加,我希望能够根据日期进行过滤。我的架构建立已取得比我想象的更加困难。是否有可能在此JSON对象排序依据一天的价值或者是有一个过滤器解决方法吗?

下面是我的JSON对象:

  {
_id:的ObjectId(53f252537d343a9ad862866c),
年 : {
    腊:[],
    十一月 : [],
    十一:[],
    九·一:[],
    八月 : [],
    七月:[
        {
            天时:21,
            头衔:事件标题
            摘要:事件摘要,
            说明:oEvent描述,
            _id:的ObjectId(53f252537d343a9ad862866d)
        }
    ]
    六一:[],
    五一:[],
    四五:[],
    三八:[],
    二月:[],
    一月 : []
},
__v:0
}

下面是已经使用了自定义过滤器按月来过滤我的看法。该排序依据是不正常,但我已经把它作为一个占位符,以显示在哪里,我想设置的功能。

 < D​​IV CLASS =calDynamic数据-NG-重复=N在[] |范围:100>
< D​​IV NG重复=CAL日历[N] .year | filterKey:一个月>
  < D​​IV NG-IF =CAL =''!>
    < D​​IV CLASS =日历>
    < D​​IV NG重复=项CAL |排序依据:'key.day'>        &所述; A HREF =/活动/ {{item.day}}>
          <物品类=eventslist>
           < D​​IV CLASS =numberedDate>
               < H3> {{item.day}}< / H3 GT&;
            < / DIV>
            < D​​IV CLASS =校准信息>
               <&H5 GT; {{item.title}}< / H5>
               &所述p为H.; {{item.summary}}&放大器; NBSP;&所述a取代;以上< / A>&下; / P>
            < / DIV>
           < /条>
      < / DIV><! - NG-VAL重复,关键的 - >
< / DIV><! - 日历 - >
< / DIV><! - NG-CAL若 - >
< / DIV><! - NG-CAL重复 - >
< / DIV><! - calDynamic - >


解决方案

您应该能够定义排序在你的对象的任何项目自定义排序功能。键位是将对象转换为在过滤函数的数组。

下面是一个例子:

  app.filter('orderByDayNumber',函数(){
  返回功能(项,场,反向){
    变种过滤= [];
    angular.forEach(项目功能(项目){
      filtered.push(项目);
    });
    filtered.sort(功能(A,B){
      返回(一个[字段]≥B〔字段​​] 1:α-1);
    });
    如果(反向)filtered.reverse();
    返回过滤;
  };
});

然后,您可以调用它是这样的:

 < D​​IV NG重复=,在CAL(键,VAL)| orderByDayNumber:'天'>

请注意,你不应该写 val.day 作为假设。

在这个伟大的博客帖子这里获取更多信息。

修改:事实上,它看起来像你的结构实际上已经是一个数组,因此,虽然这种技术仍然可以工作,它可能没有必要 - 它可能刚刚被你添加的方式该参数排序依据是造成问题。

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}}&nbsp;<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.

这篇关于角排序依据对象可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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