简体   繁体   中英

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:

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

Datepicker 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.

I added a listener to the controller that converts the new date to JSON and this fixed it for me:

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

(and filtered using the new dateString value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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