Question

I am completely new to AngularJS. Here is my HTML code

<div ng-controller="DateRangeCtrl">

      <div class="container">
        <div class="form-horizontal">
          <input type="text" datepicker-popup="MM-dd-yyyy" ng-model="dt1" is-open="opened1" max="maxFromDate" ng-change="setMinToDate()"/>
          <button class="btn btn-sm" ng-click="open1($event)"><span class="glyphicon glyphicon-calendar"></span></button>
        </div>
        <div class="form-horizontal">
            <input type="text" datepicker-popup="MM-dd-yyyy" ng-model="dt2" is-open="opened2" min="minToDate" max="maxToDate" ng-change="filterDateAdded()"/>
            <button class="btn btn-sm" ng-click="open2($event)"><span class="glyphicon glyphicon-calendar"></span></button>
        </div>

        <p><strong>Selected From Date: </strong> {{dt1 | date:'mediumDate'}}</p>
        <p><strong>Selected To Date: </strong> {{dt2 | date:'mediumDate'}}</p>
      </div>
      <hr />
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>ID</th>
            <th>Date Added</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="item in items | orderBy:mySortFunction ">
            <td>{{item.ID}}</td>
            <td>{{ parseDate(item.dateAdded) | date:'longDate'}}</td>
          </tr>
        </tbody>
      </table>

    </div>

The following is my angular code:

testApp.config(function (datepickerConfig, datepickerPopupConfig) {
          datepickerConfig.showWeeks = false;
          datepickerPopupConfig.showButtonBar = false;
    });  

    testApp.controller('DateRangeCtrl', function($scope) {

        $scope.items = [
          {ID: "1", dateAdded: "01-04-2013"},
          {ID: "2", dateAdded: "12-01-2013"},
          {ID: "3", dateAdded: "12-31-2013"},
          {ID: "4", dateAdded: "01-12-2014"},
          {ID: "5", dateAdded: "03-04-2014"}

        ];

        $scope.parseDate = function(input){
            var parts = input.split('-');
            var newParts = new Date(parts[2], parts[0]-1, parts[1]); // Note: months are 0-based
            return newParts;
        }


        $scope.open1 = function($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.opened1 = true;
       };

      $scope.maxFromDate = new Date();

      $scope.maxToDate = new Date();

      $scope.setMinToDate = function (){
        $scope.dt2 = null;
        $scope.minToDate = $scope.dt1;
      };

      $scope.open2 = function($event) {
      $event.preventDefault();
      $event.stopPropagation();

      $scope.opened2 = true;
      };
    });

How can i filter rows based on selected dates? For e.g. if i select "01/01/2014" in the "From" datePicker then i should be able to see all the rows whose "Date Added" column has value more than "January 1, 2014". The output will be rows with ID: 4 and 5.

It should behave the literal equivalent way when the "To" datePicker is selected

Please help me out. i'm stuck!

Update: HTML

<tr ng-repeat="item in items | filter:dateFilter ">

JS

$scope.dateFilter = function (item) {
          return (item.dateAdded < $scope.dt2);
      };

Am i doing anything wrong? It still doesn't work....

Was it helpful?

Solution

By some hit n trial method, i eventually got it right. The problem was the "item.dateAdded" string had to be converted to Date object. Solution is adding the following piece of code in the js file:

$scope.filterDateAdded = function (){
if($scope.dt1 != null)
{
  $scope.dateFilter = function (item) {
    return ($scope.parseDate(item.dateAdded) >= $scope.dt1 && $scope.parseDate(item.dateAdded) <= $scope.dt2);
  };
}

};

Cheers!

OTHER TIPS

Change your code like this

    <input type="text" datepicker-popup="MM-dd-yyyy" ng-model="dt2" is-open="opened2" min="minToDate" max="maxToDate" ng-model="search" ng-change="filterDateAdded()"/>


     <tr ng-repeat="item in items | filter:dateFilter">



 $scope.filterDateAdded=function()
    {

                 $scope.dateFilter = function () {
                  var result=[];
                   for(var i in $scope.item)
                    {  if($scope.item[i].dateAdded >$scope.search)
                           {
                                   result.push($scope.item[i]);
                                 }

                           }
              return result;
          };
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top