在角度js datePicker中禁用SPECIFIC DATES(而不是dateTimePicker)

时间:2016-03-09 07:22:56

标签: angularjs datepicker

我正在使用ui.bootstrap.datePicker。我有以下代码,使用填充日期的数组禁用特定日期禁用!现在问题是它在plunker上在线工作,当我在我的本地xampp服务器(v3.2.1)上使用相同的代码时,它不会禁用日期。关于为什么会发生这种情况的任何建议?

我也会附上plunker片段,以便evryone知道它在线工作正常

siteDatePicker.js:

        var myApp= angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap', 'ui.bootstrap.dateparser', 'ui.bootstrap.datepicker']);



                     myApp.controller('DatepickerDemoCtrl', function($scope, $http) {



                    $scope.today = function() {
                        $scope.dt = new Date();
                      };
                      $scope.today();

                      $scope.clear = function() {
                        $scope.dt = null;
                      };

                      $scope.inlineOptions = {
                        customClass: getDayClass,
                        minDate: new Date(),
                        showWeeks: false
                      };

                      $scope.dateOptions = {
                          formatYear: 'yy',
                          maxDate: new Date(2020, 5, 22),
                          minDate: new Date(),
                          startingDay: 1
                        };

                        // Disable weekend selection
                        function disabledasda(data) {
                          var date = data.date,
                            mode = data.mode;
                          return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
                        }

                      $scope.toggleMin = function() {
                        $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
                        $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
                      };

                      $scope.toggleMin();

                      $scope.open1 = function() {
                        $scope.popup1.opened = true;
                      };


                      $scope.setDate = function(year, month, day) {
                        $scope.dt = new Date(year, month, day);
                      };

                      $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
                      $scope.format = $scope.formats[0];
                      $scope.altInputFormats = ['M!/d!/yyyy'];

                      $scope.popup1 = {
                        opened: false
                      };


                      var tomorrow = new Date();
                      tomorrow.setDate(tomorrow.getDate() + 1);
                      var afterTomorrow = new Date();
                      afterTomorrow.setDate(tomorrow.getDate() + 1);
                      $scope.events = [
                        {
                          date: tomorrow,
                          status: 'full'
                        },
                        {
                          date: afterTomorrow,
                          status: 'partially'
                        }
                      ];
                      $scope.disabled = function(date, mode){

                        var holidays = [
                          new Date(2016,2,14),
                          new Date(2016,2,15),
                          new Date(2016,2,16),
                        ]

                        var isHoliday = false;
                        for(var i = 0; i < holidays.length ; i++) {
                          if(areDatesEqual(holidays[i], date)){
                            isHoliday = true;
                          }
                        }

                        return ( mode === 'day' && isHoliday );
                      };

                      function areDatesEqual(date1, date2) {

                        return date1.getDate() == date2.getDate() &&
                               date1.getMonth() == date2.getMonth() &&
                               date1.getFullYear() == date2.getFullYear();

                      }

                      function getDayClass(data) {
                        var date = data.date,
                          mode = data.mode;
                        if (mode === 'day') {
                          var dayToCheck = new Date(date).setHours(0,0,0,0);

                          for (var i = 0; i < $scope.events.length; i++) {
                            var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);

                            if (dayToCheck === currentDay) {
                              return $scope.events[i].status;
                            }
                          }
                        }

                        return '';
                      }



                     });

siteDatePicker.html:

            <head>

              <title>SiteDatePicker</title>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1">
              <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
                  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
                <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
                <script src="./ui-bootstrap-tpls-1.2.1.min.js"></script>
              <script src="./siteDatePicker.js"></script>






            </head>


               <body ng-app="ui.bootstrap.demo" >
                <style>
                  .full button span {
                    background-color: limegreen;
                    border-radius: 32px;
                    color: black;
                  }
                  .partially button span {
                    background-color: orange;
                    border-radius: 32px;
                    color: black;
                  }
                </style>
                <div ng-controller="DatepickerDemoCtrl">
                  <pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>

                  <h4>Popup</h4>
                  <div class="row">
                    <div class="col-md-6">
                      <p class="input-group">
                        <input type="text" class="form-control"  uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" date-disabled="disabled(date, mode)" />
                        <span class="input-group-btn">
                          <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
                        </span>
                      </p>
                    </div>
                  </div>
                </div>
              </body>
            </html>

var app = angular.module('plunker', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope,$q) {
  $scope.name = 'World';
  $scope.values = {
    date1: new Date()
  };

  var dateDisableDeferred =  $q.defer();
  $scope.dateDisablePromise = dateDisableDeferred.promise;
  
  var currentDay = new Date().getDay();
  var disableModeIsLessThan = true;
  
  $scope.toggleDisableMode = function() {
    dateDisableDeferred.notify(new Date().getTime());
    disableModeIsLessThan = !disableModeIsLessThan;
  };
  
  
  $scope.disabled = function(date, mode){

            var holidays = [
              new Date(2016,2,14),
              new Date(2016,2,15),
              new Date(2016,2,16),
            ]

            var isHoliday = false;
            for(var i = 0; i < holidays.length ; i++) {
              if(areDatesEqual(holidays[i], date)){
                isHoliday = true;
              }
            }

            return ( mode === 'day' && isHoliday );
          };

          function areDatesEqual(date1, date2) {

            return date1.getDate() == date2.getDate() &&
                   date1.getMonth() == date2.getMonth() &&
                   date1.getFullYear() == date2.getFullYear();

          }



});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.25/angular.js" data-semver="1.2.25"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <div class="container padTop">
      <p>Hello {{name}}!</p>

    <div>
      <div style="display:inline-block; min-height:290px;">
          <datepicker 
              ng-model="values.date1" 
              min-date="minDate" 
              show-weeks="true" 
             date-disabled="disabled(date, mode)"
              class="well well-sm"></datepicker>
      </div>
    </div>
  
   

    </div>
    
    
  </body>

</html>

1 个答案:

答案 0 :(得分:1)

因为我没有足够的评论点。因此,我正在写评论作为ans。

克莱德只是检查这两个文件是否正确加载。

          <script src="./ui-bootstrap-tpls-1.2.1.min.js"></script>
          <script src="./siteDatePicker.js"></script>

按F12和网络标签,查看它们是否已加载。

相关问题