kipris
kipris

Reputation: 3039

AngularUI: range datepicker don't block dates

Can anybody help?
I wanted in my start-datepicker blocked dates after end-date and blocked dates in end-datepicker before start-date. I used angularui datepickers.
I tried to do this using this code:

  $scope.start = new Date();
  $scope.end = new Date($scope.start.getTime() + 7 * 24 * 60 * 60 * 1000);

  $scope.minStartDate = 0; //fixed date
  $scope.maxStartDate = $scope.end; //init value
  $scope.minEndDate = $scope.start; //init value
  $scope.maxEndDate = $scope.end; //fixed date same as $scope.maxStartDate init value

  $scope.$watch('start', function(v){
    $scope.minEndDate = v;
  });
  $scope.$watch('end', function(v){
    $scope.maxStartDate = v;
  });

And here's plunker: http://plnkr.co/edit/tTBcSZE08VYCpitdhCRA?p=preview

Upvotes: 0

Views: 58

Answers (1)

ngCoder
ngCoder

Reputation: 2105

I have updated your plunker with the necessary changes, basically you were setting minDate and maxDate incorrectly.You have declared date options but there is no such object in your controller, so I've added two date-options object for the calendars respectively. Here is the working code.

Changes in your JS are as below

    $scope.datePicker ={};
    $scope.start = new Date();
    $scope.end = new Date($scope.start.getTime() + 7 * 24 * 60 * 60 * 1000);

    $scope.datePicker.minStartDate = 0; //fixed date
    $scope.datePicker.maxStartDate = $scope.end; //init value
    $scope.datePicker.minEndDate = $scope.start; //init value
    $scope.datePicker.maxEndDate = $scope.end; //fixed date same as $scope.maxStartDate init value

$scope.$watch('start', function(v) {
            $scope.datePicker.minEndDate = v;
            $scope.dateOptions2.minDate = v; //chabge the same in date options object to reflect in UI
        });
        $scope.$watch('end', function(v) {
            $scope.datePicker.maxStartDate = v;
            $scope.dateOptions1.maxDate = v;
        });
    //create two separate objects for date options where you can set ,in and max date..
        $scope.dateOptions1 = {
            //dateDisabled: disabled,
            formatYear: 'yyyy',
            maxDate: $scope.datePicker.maxStartDate,
            minDate: $scope.datePicker.minStartDate,
            startingDay: 1
        };
         $scope.dateOptions2 = {
            //dateDisabled: disabled,
            formatYear: 'yyyy',
            maxDate: $scope.datePicker.maxEndDate,
            minDate: $scope.datePicker.minEndDate,
            startingDay: 1
        };

Changes in your HTML as below

<div ng-controller="DatepickerPopupDemoCtrl">
        <p class="input-group">
          <input type="text" 
                  class="form-control" 
                  uib-datepicker-popup="{{format}}" 
                  ng-model="start" 
                  is-open="popup1.opened" 

                  datepicker-options="dateOptions1" 
                  close-text="Close" 
                  alt-input-formats="altInputFormats" />
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open1()">
              <i class="glyphicon glyphicon-calendar"></i>
            </button>
          </span>
        </p>

        <hr>

        <p class="input-group">
          <input type="text" 
                  class="form-control" 
                  uib-datepicker-popup="{{format}}" 
                  ng-model="end" 
                  is-open="popup2.opened" 

                  datepicker-options="dateOptions2" 
                  close-text="Close" 
                  alt-input-formats="altInputFormats" />
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open2()">
              <i class="glyphicon glyphicon-calendar"></i>
            </button>
          </span>
        </p>

  </div>

Upvotes: 1

Related Questions