NG-重复使用过滤器比较当前日期? [英] NG-Repeat compare to current date using filter?

查看:107
本文介绍了NG-重复使用过滤器比较当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个包含日期的列表中的对象,我遍历它像这样:

 <选择NG点击=monthpicker()>
<选项类=动画 - 重复NG重复=returnpicker在monthpicker(alldatesdump)VALUE ={{returnpicker.date}}> {{returnpicker.date |日期:'YYYY-MMM'}}< /选项>
< /选择>

采用NG-重复此返回以下

 <选择NG点击=monthpicker()>
  < - ngRepeat:在monthpicker(alldatesdump)returnpicker - >
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>
  <选项类=动画重复NG-范围NG结合NG重复=returnpicker在monthpicker(alldatesdump)VALUE =2013 - 11月> 2013 - 11月< /选项>

等为对象中每个月现在,这是因为它返回一个月份名称,一年的每一天中的对象。
好了,所以我有两个问题:
1:我如何筛选的结果,使他们只返回每个月的名字和年份的一个例子吗?
2:如何设置它,因此它从目前的月份只返回?
我想只有achive这一点使用AngularJS的过滤器,但我这样做,如果需要有jQuery的用!
********UPDATE**************
继承人在我的JS文件在当前范围的项目:

  scope.monthpicker =功能(alldatesdump){变种alldatesdump = booking.getalldates();
对(在alldatesdump VAR日期){/ *
    如果(alldatesdump.hasOwnProperty(日期)){
        的console.log(日期);
    }
}
对(在alldatesdump VAR日期){
   VAR OBJ = alldatesdump [日期];
   对于(OBJ中的道具VAR){
      //重要的检查,这是物体自己的财产
      //不从原型继承道具
      如果(obj.hasOwnProperty(丙)){
        的console.log(丙+=+物镜[丙]);
      }
   }
} * /
返回alldatesdump;
};


解决方案

下面是我如何使用过滤器只显示订单的最后两天为例

  .filter('getOrders',函数(){
  返回功能(订单){    变种filtered_list = [];    对于(VAR I = 0; I< orders.length;我++){      VAR two_days_ago =新的日期()的getTime() - 2 * 24 * 60 * 60 * 1000。
      VAR LAST_MODIFIED =新的日期(订单[I] .last_modified).getTime();      如果(two_days_ago< = LAST_MODIFIED){
        filtered_list.push(订单[I]);
      }
    }
    返回filtered_list;
  }
});

DOM看起来像这样

 < TR NG重复=为了订单| getOrders>

希望这有助于小提琴的jsfiddle

Ok I have an object that contains a list of dates and I'm traversing it like so:

<select ng-click="monthpicker()" >
<option class="animate-repeat" ng-repeat="returnpicker in monthpicker(alldatesdump)"      value="{{returnpicker.date}}">{{returnpicker.date  | date:'yyyy-MMM' }}</option> 
</select>

using ng-repeat this returns the following:

<select ng-click="monthpicker()">
  <!-- ngRepeat: returnpicker in monthpicker(alldatesdump) -->
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>
  <option class="animate-repeat ng-scope ng-binding" ng-repeat="returnpicker in monthpicker(alldatesdump)" value="2013-Nov">2013-Nov</option>

and so on for each month in the object now this is because it's returning a month name and a year for each day in the object. Ok so i have two questions: 1: How do i filter the results so they only return one example of each month name and the year? 2: how do i set it so it returns only from the current month? I'd like to achive this using AngularJS filters only but i do have jquery avail if needed! ********UPDATE************** heres the current scope item in my JS file:

scope.monthpicker = function(alldatesdump){

var alldatesdump = booking.getalldates();
/*for (var date in alldatesdump){
    if (alldatesdump.hasOwnProperty(date)){
        console.log(date);
    }
}
for (var date in alldatesdump) {
   var obj = alldatesdump[date];
   for (var prop in obj) {
      // important check that this is objects own property 
      // not from prototype prop inherited
      if(obj.hasOwnProperty(prop)){
        console.log(prop + " = " + obj[prop]);
      }
   }
}*/
return alldatesdump;
}; 

解决方案

Here is an example of how I used a filter to only show orders from the last two days

.filter('getOrders', function() {
  return function (orders) {

    var filtered_list = [];

    for (var i = 0; i < orders.length; i++) {

      var two_days_ago = new Date().getTime() - 2*24*60*60*1000;
      var last_modified = new Date(orders[i].last_modified).getTime();

      if (two_days_ago <= last_modified) {
        filtered_list.push(orders[i]);
      }
    }
    return filtered_list;
  }
});

DOM looks like this

<tr ng-repeat="order in orders|getOrders">

Hopefully this fiddle helps JSFiddle

这篇关于NG-重复使用过滤器比较当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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