【问题标题】:year and month only datepicker angularjs and bootstrap仅限年月日期选择器 angularjs 和引导程序
【发布时间】:2016-03-03 08:38:23
【问题描述】:

我有问题我想从日期到日期使用日期选择器(签入和签出月份而不是天数),它应该是月份,问题是:我有模板,但它显示了日期和年份还有月份,我想要的只是月份和年份

这是我得到的,我只想要几个月和一年

http://plnkr.co/edit/W5pb1boMLOHZFnWkMU8o?p=preview

html:

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.7.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="DatepickerDemoCtrl">
    <h4>Start</h4>
    <div class="form-horizontal">
        <p>
            <input type="text" datepicker-popup="{{format}}" ng-model="start" is-open="startOpened" min="minStartDate" max="maxStartDate" datepicker-options="dateOptions" close-text="Close" />
            <button class="btn" ng-click="openStart()"><i class="icon-calendar"></i></button>
        </p>
    </div>
    <h4>End</h4>
    <div class="form-horizontal">
        <p>
            <input type="text" datepicker-popup="{{format}}" ng-model="end" is-open="endOpened" min="minEndDate" max="maxEndDate" datepicker-options="dateOptions" close-text="Close" />
            <button class="btn" ng-click="openEnd()"><i class="icon-calendar"></i></button>
        </p>
    </div>
</div>
  </body>
</html>

js:

angular.module('plunker', ['ui.bootstrap']);
var DatepickerDemoCtrl = function ($scope, $timeout) {
  $scope.start = new Date('11/20/13');
  $scope.end = new Date();

  $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;
  });

  $scope.openStart = function() {
    $timeout(function() {
      $scope.startOpened = true;
    });
  };

  $scope.openEnd = function() {
    $timeout(function() {
      $scope.endOpened = true;
    });
  };

  $scope.dateOptions = {
    'year-format': "'yy'",
    'starting-day': 1
  };
};

提前致谢

【问题讨论】:

  • 只需在控制器中添加`$scope.format = 'MM-yyyy'`
  • 我试过不工作你可以编辑它对我不起作用

标签: javascript html angularjs twitter-bootstrap angular-ui-bootstrap


【解决方案1】:

尝试在日期选择器元素中使用 min-modemax-mode 属性。通过将 min-mode 设置为 month 并将 max-mode 设置为 year 您仅启用月份选择器和年份选择器。

HTML:

<input type="text" show-weeks="false" show-button-bar="false" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="status.opened" min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" min-mode="'month'" max-mode="'year'" />

这应该会有所帮助。

您还可以在我仅启用月份选择器的此线程上查看我的答案。 (similar solution)

demo

【讨论】:

  • 它不起作用,您附加的解决方案,我试图将按钮添加到我的解决方案中但不起作用,感谢您的帮助
  • 您尝试过演示吗?那行得通。指定什么不起作用。
  • 是的,谢谢,但可以保存月份和年份
  • 将 max-mode 更改为 year。只需要一个月和一年。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
相关资源
最近更新 更多