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

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

问题描述

好的,我有一个包含日期列表的对象,我正在遍历它:

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>

使用 ng-repeat 返回以下内容:

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>

等等现在对象中的每个月,这是因为它返回对象中每一天的月份名称和年份.好的,所以我有两个问题:1:如何过滤结果,使它们只返回每个月份名称和年份的一个示例?2:我如何设置它以便它只从当月返回?我想仅使用 AngularJS 过滤器来实现此目的,但如果需要,我确实可以使用 jquery!********更新**************这是我的 JS 文件中的当前范围项:

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 看起来像这样

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

希望这个小提琴有助于 JSFiddle

Hopefully this fiddle helps JSFiddle

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

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