0

我在我的项目中使用 Angular UI Bootstrap datepicker 指令。我有这个特定的需求,比如我只需要从当天开始启用 5 天。如果是周末,我需要禁用它们并启用剩余的日子。例如,如果今天是星期五,我需要启用 fri, mon, tue, web, thurs。我正在使用 dateDisabled 属性来实现这一点。问题是所有过去一个月的日期也都被启用了。另外我认为我提出的解决方案并不优雅。下面是我的标记和代码。请帮助我。先感谢您。

 <input show-weeks="false" ng-click="vm.openDate()" name="quotedate" type="text" class="form-control" ng-model-options="{timezone:'UTC'}" uib-datepicker-popup="dd/MM/yyyy" ng-model="vm.quote.date" is-open="vm.quotedate.opened" datepicker-options="vm.dateOptions" required close-text="Close" readonly="true"/> 

vm.dateOptions = {
     dateDisabled: disabled,
     minDate: today            
};

function disabled(data) {
        var date = data.date,
            mode = data.mode;
        if (today.getDay() === 0 || today.getDay() === 6) {
            return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6 || date.getDate() > today.getDate() + 5 );
        }else if (today.getDay() === 1) {
            return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6 || date.getDate() > today.getDate() + 4 );
        }else {
            return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6 || date.getDate() > today.getDate() + 6 );
        }
}
4

1 回答 1

0

您正在寻找的是min-date,max-datedate-disabled属性,如文档中所述。此示例中的date-disabled函数直接从文档中提取,为了限制可用的日期范围,只需将min-date属性设置为当前日期时间:

vm.dt = new Date();
vm.minDate = angular.copy(vm.dt);

...以及max-date从现在开始的五天:

var fiveDaysFromNow = angular.copy(vm.dt);
fiveDaysFromNow.setDate(fiveDaysFromNow.getDate() + 5);
vm.maxDate = fiveDaysFromNow;

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($scope) {
  
  var vm = this;
  
  function today() {
    vm.dt = new Date();
  }
  
  today(); 
  
  vm.opened = false;

  vm.openDatePopup = function() {
    vm.opened = true;
  };

  // Disable weekend selection
  vm.disabled = function(date, mode) {
    return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
  };
  
  vm.minDate = angular.copy(vm.dt);
  
  var fiveWeekdaysFromNow = angular.copy(vm.dt);
  fiveWeekdaysFromNow.setDate(fiveWeekdaysFromNow.getDate() + 7);
  vm.maxDate = fiveWeekdaysFromNow;

  vm.dateOptions = {
    formatYear: 'yy',
    startingDay: 0
  };

  vm.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
  vm.format = vm.formats[0];

});
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.1.0.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

    <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 as demo">
      <div class="row">
        <div class="col-xs-8 col-xs-offset-2">
          <p class="input-group">
            <input type="text" 
                   class="form-control" 
                   uib-datepicker-popup="{{demo.format}}" 
                   ng-model="demo.dt" 
                   show-weeks="false"
                   is-open="demo.opened" 
                   min-date="demo.minDate" 
                   max-date="demo.maxDate" 
                   datepicker-options="demo.dateOptions" 
                   date-disabled="demo.disabled(date, mode)" 
                   ng-required="true" 
                   close-text="Close"/>
            <span class="input-group-btn">
              <button type="button" 
                      class="btn btn-default" 
                      ng-click="demo.openDatePopup()">
                <i class="glyphicon glyphicon-calendar"></i>
              </button>
            </span>
          </p>
        </div>
      </div>
    </div>
  </body>
</html>
希望这可以帮助!

于 2016-05-24T02:44:00.090 回答