日期范围过滤器无效

时间:2017-11-09 02:58:31

标签: javascript angularjs filter

我似乎无法使此日期范围filter起作用?当我选择一个日期时,它似乎会触发,但它没有推动var filter

中的任何内容

我已经解决了这个问题好几个小时而且无处可去。

HTML

<div class="form-group">
   <input type="date" class="form-control" ng-model="from">
</div>
<div class="form-group">
   <input type="date" class="form-control" ng-model="to">
</div>

纳克重复

<tr ng-repeat="attendance in displayedCollection | dateRange:from:to ">
 <td>{{ attendance.day | date: 'EEEE, MMM d'}}</td>
 <td>{{ attendance.timeIn | date:'h:mm a' }}</td>
 <td>{{ attendance.timeOut | date:'h:mm a'}}</td>
 <td class="text-capitalize">
 <i class="fa fa-map-marker" aria-hidden="true"></i> {{ attendance.details }}</td>
 <td>{{ attendance.totalHrs | date:'hh:mm'}} </td>
</tr>

控制器

.filter('dateRange', function () {
  return function (product, fromDate, toDate) {
    var filtered = [];

    if (!fromDate && !toDate) {
      return product;
    }

    var from_date = Date.parse(fromDate);
    var to_date = Date.parse(toDate);

    angular.forEach(product, function (item) {
      if (item.day > from_date && item.day < to_date) {
        filtered.push(item); //not pushing anything
      }
    });
    return filtered; // returning nothing
  };
})

编辑:

displayedCollection数据

[{
    "_id": "5a03bed5c349e82fa4937430",
    "details": "MHQ Lucena Branch 2",
    "fullName": "Priz almarinez",
    "timeIn": "2017-11-09T02:34:57.000Z",
    "attendance": "admin11-09-2017",
    "day": "2017-11-08T16:00:00.000Z",
    "user": "admin",
    "__v": 0,
    "status": "Pending",
    "check": false
},
{
    "_id": "5a03c4e61bebc52c0737a426",
    "details": "MHQ Lucena Branch 2",
    "fullName": "Priz almarinez",
    "timeIn": "2017-11-09T02:34:57.000Z",
    "attendance": "admin11-09-2017",
    "day": "2017-11-09T16:00:00.000Z",
    "user": "admin",
    "__v": 0,
    "status": "Pending",
    "check": false
},
{
    "_id": "5a03c4ec1bebc52c0737a427",
    "details": "MHQ Lucena Branch 2",
    "fullName": "Priz almarinez",
    "timeIn": "2017-11-09T02:34:57.000Z",
    "attendance": "admin11-09-2017",
    "day": "2017-11-10T16:00:00.000Z",
    "user": "admin",
    "__v": 0,
    "status": "Pending",
    "check": false
}]

由于

1 个答案:

答案 0 :(得分:1)

您目前正在将ng-false-value item.day(&#34; 2017-11-10T16:00:00.000Z&#34;)与解析日期TimeString进行比较。解析forEach循环中的Integer并进行比较。按预期工作。下面是工作样本https://codepen.io/pablo-tavarez/pen/pdRvjW?editors=0010

的链接