简体   繁体   English

按日期的Angular JS过滤器不起作用

[英]Angular JS filter by date not working

I'm trying to filter the data in a table so that it only shows me the items entered on a certain date, but I can't seem to get this working. 我试图过滤表中的数据,以便仅显示在特定日期输入的项目,但似乎无法正常工作。 I can see the model being updated when I select a new date, but the data in the table doesn't change. 当我选择一个新日期时,我可以看到该模型正在更新,但是表中的数据没有更改。

HTML: HTML:

 <tr ng-repeat='file in files  | filter: date '>

Datepicker HTML: 日期选择器HTML:

<label>From:</label> 
  <p class="input-group" style="width:200px">
    <input type="text" class="form-control" uib-datepicker-popup = "{{format}}" ng-model="date" is-open="status.opened"
                                       min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)"
                                       ng-required="true" close-text="Close"/>
        <span class="input-group-btn">
          <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
        </span>
  </p>

Controller: 控制器:

(function ()
 {
  'use strict';

 angular.module('aml-tech-dashboard.successful-emails').controller('datepickerController', function ($scope) {
$scope.date = '';
$scope.status = {
    opened: false
};

$scope.format = "yyyy-MM-dd"
$scope.minDate = new Date();
$scope.dateOptions = {
    //formatYear: 'yy',
    startingDay: 1
};

$scope.open = function ($event) {
    $scope.status.opened = true;

};

$scope.date = new Date();

});

})();

You should use object for filtering. 您应该使用对象进行过滤。

<tr ng-repeat='file in files  | filter: date '>

$scope.date = {date: '2016-08-01'}

And every 'file' must have field 'date' 并且每个“文件”都必须具有“日期”字段

So I figured out the problem. 所以我找出了问题所在。 The files were in a JSON format and the date from the datepicker was a Date object. 文件采用JSON格式,日期选择器中的日期为Date对象。

I added a listener to the controller that converts the new date to JSON and this fixed it for me: 我向控制器添加了一个侦听器,该侦听器将新日期转换为JSON,并为我修复了该问题:

 $scope.$watch("date", function(newval, oldval) {
        $scope.dateString = $scope.date.toJSON();
    });

(and filtered using the new dateString value) (并使用新的dateString值进行过滤)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM