覆盖 AngularJs DatePicker 弹出窗口以添加新标题 [英] Overriding AngularJs DatePicker popup to add new title

查看:17
本文介绍了覆盖 AngularJs DatePicker 弹出窗口以添加新标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明了一切.

我一直在寻找高&低,看不到方法,但是,在我破解模板之前,我想我会在这里问.

只是为了说清楚 - 我希望能够在日期选择器的顶部添加一些文本(这是一个弹出窗口,如果有什么不同的话),例如你的生日是什么时候?".

解决方案

如果你关注这个主题,有很多方法可以做到这一点:你能在 AngularUI Bootstrap 中覆盖特定的模板吗? 但我只限于两个,其中一个是 CSS.

模板覆盖:

Plunker 演示

弹出文件的默认文件是template/datepicker/popup.html,您只需要复制文件的内容并附加新代码即可.新代码将被包裹在 <script id="template/datepicker/popup.html" type="text/ng-template"> directive 并放置在

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function($scope) {$scope.today = function() {$scope.dt = new Date();};$scope.today();$scope.clear = function() {$scope.dt = null;};//禁用周末选择$scope.disabled = 函数(日期,模式){return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));};$scope.toggleMin = function() {$scope.minDate = $scope.minDate ?空:新日期();};$scope.toggleMin();$scope.maxDate = 新日期(2020, 5, 22);$scope.open = function($event) {$scope.status.opened = true;};$scope.setDate = 函数(年、月、日){$scope.dt = 新日期(年、月、日);};$scope.dateOptions = {formatYear: 'yy',起始日:1};$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];$scope.format = $scope.formats[0];$scope.status = {打开:假};var 明天 = 新日期();明天.setDate(tomorrow.getDate() + 1);var afterTomorrow = new Date();afterTomorrow.setDate(tomorrow.getDate() + 2);$scope.events = [{日期:明天,状态:'满'}, {日期:明天之后,状态:'部分'}];$scope.getDayClass = 函数(日期,模式){如果(模式 === '天'){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) {返回 $scope.events[i].status;}}}返回 '​​';};});angular.module("template/alert/alert.html", []).run(["$templateCache",函数($templateCache){$templateCache.put("template/alert/alert.html"," <div class='alert' ng-class='type && \"alert-\" + type'>\n" +" <button ng-show='closeable' type='button' class='close' ng-click='close()'>Close</button>\n" +" <div ng-transclude></div>\n" +" </div>");}]);

.popup-header {文本对齐:居中;字体粗细:粗体;填充:10px;}

<html ng-app="ui.bootstrap.demo"><头><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script><script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script><script src="example.js"></script><link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="样式表"><link href="style.css" rel="stylesheet"><!-- 覆盖代码--><script id="template/datepicker/popup.html" type="text/ng-template"><ul class="uib-datepicker-popup dropdown-menu" dropdown-nested ng-if="isOpen" style="display:block" ng-style="{top: position.top+'px', left: position.left+'px'}" ng-keydown="keydown($event)" ng-click="$event.stopPropagation()"><!-- 额外代码--><div class="popup-header">你的生日是什么时候?</div><!-- 额外代码--><li ng-transclude></li><li ng-if="showButtonBar" style="padding:10px 9px 2px" class="uib-button-bar"><span class="btn-group pull-left"><button type="button" class="btn btn-sm btn-info uib-datepicker-current" ng-click="select('today')" ng-disabled="isDisabled('today')">{{ getText('current') }}</button><button type="button" class="btn btn-sm btn-danger uib-clear" ng-click="select(null)">{{ getText('clear') }}</button></span><button type="button" class="btn btn-sm btn-success pull-right uib-close" ng-click="close()">{{ getText('close') }}</button><!-- 覆盖代码--><身体><div ng-controller="DatepickerDemoCtrl"><div class="row"><div class="col-xs-offset-3 col-xs-6 col-md-3"><h4 class="text-center">日期选择器弹出窗口</h4><p class="输入组"><input type="text" class="form-control date-picker" uib-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"close-text="关闭"/><span class="input-group-btn"><button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></按钮></span></p>

</html>

CSS

这是一种非标准方式,但无论如何它都不会干扰您的模板结构.

Plunker 演示

CSS 中的

:before 伪元素支持 content 属性来创建文本.由于您不需要添加标题文本/标题以外的任何内容,因此这是一个合适的解决方案.检查 Plunkr 中的 style.css 文件以编辑文本.

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function($scope) {$scope.today = function() {$scope.dt = new Date();};$scope.today();$scope.clear = function() {$scope.dt = null;};//禁用周末选择$scope.disabled = 函数(日期,模式){return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));};$scope.toggleMin = function() {$scope.minDate = $scope.minDate ?空:新日期();};$scope.toggleMin();$scope.maxDate = 新日期(2020, 5, 22);$scope.open = function($event) {$scope.status.opened = true;};$scope.setDate = 函数(年、月、日){$scope.dt = 新日期(年、月、日);};$scope.dateOptions = {formatYear: 'yy',起始日:1};$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];$scope.format = $scope.formats[0];$scope.status = {打开:假};var 明天 = 新日期();明天.setDate(tomorrow.getDate() + 1);var afterTomorrow = new Date();afterTomorrow.setDate(tomorrow.getDate() + 2);$scope.events = [{日期:明天,状态:'满'}, {日期:明天之后,状态:'部分'}];$scope.getDayClass = 函数(日期,模式){如果(模式 === '天'){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) {返回 $scope.events[i].status;}}}返回 '​​';};});

.date-picker + .dropdown-menu::before {内容:你的生日是什么时候?";字体粗细:粗体;位置:相对;左:25%;}.date-picker + .dropdown-menu li {边距顶部:5px;}

<html ng-app="ui.bootstrap.demo"><头><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script><script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script><script src="example.js"></script><link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="样式表"><link href="style.css" rel="stylesheet"><身体><div ng-controller="DatepickerDemoCtrl"><div class="row"><div class="col-xs-offset-3 col-xs-6 col-sm-offset-4 col-sm-4 col-md-3"><h4 class="text-center">日期选择器弹出窗口</h4><p class="输入组"><input type="text" class="form-control date-picker" uib-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"close-text="关闭"/><span class="input-group-btn"><button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></按钮></span></p>

</html>

The question says it all.

I have hunted high & low and don't see a way, but, before I hack the template I thought that I would ask here.

Just to make it clear - I want to be able to add some text at the top of the datepicker (which is a popup, if that makes any difference) such as "When is your birthday?".

解决方案

There are many ways to do this if you follow this topic: Can you override specific templates in AngularUI Bootstrap? but I will just limit to two, one of which is CSS.

Template overriding:

Plunker Demo

The default file for the popup file is template/datepicker/popup.html, you just need to copy the contents of the file and append new code to it. The new code then will be wrapped inside <script id="template/datepicker/popup.html" type="text/ng-template"> directive and placed within <head>

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

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

  // Disable weekend selection
  $scope.disabled = function(date, mode) {
    return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
  };

  $scope.toggleMin = function() {
    $scope.minDate = $scope.minDate ? null : new Date();
  };
  $scope.toggleMin();
  $scope.maxDate = new Date(2020, 5, 22);

  $scope.open = function($event) {
    $scope.status.opened = true;
  };

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

  $scope.dateOptions = {
    formatYear: 'yy',
    startingDay: 1
  };

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

  $scope.status = {
    opened: false
  };

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

  $scope.getDayClass = function(date, 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 '';
  };
});

angular.module("template/alert/alert.html", []).run(["$templateCache",
  function($templateCache) {
    $templateCache.put("template/alert/alert.html",
      "      <div class='alert' ng-class='type && \"alert-\" + type'>\n" +
      "          <button ng-show='closeable' type='button' class='close' ng-click='close()'>Close</button>\n" +
      "          <div ng-transclude></div>\n" +
      "      </div>");
  }
]);

.popup-header {
  text-align: center;
  font-weight: bold;
  padding: 10px;
}

<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
  <script src="example.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  <link href="style.css" rel="stylesheet">

  <!-- Overriding code -->

  <script id="template/datepicker/popup.html" type="text/ng-template">
    <ul class="uib-datepicker-popup dropdown-menu" dropdown-nested ng-if="isOpen" style="display: block" ng-style="{top: position.top+'px', left: position.left+'px'}" ng-keydown="keydown($event)" ng-click="$event.stopPropagation()">
      <!-- Extra code -->

      <div class="popup-header">When is your Birthday?</div>

      <!-- Extra code -->
      <li ng-transclude></li>
      <li ng-if="showButtonBar" style="padding:10px 9px 2px" class="uib-button-bar">
        <span class="btn-group pull-left">
			<button type="button" class="btn btn-sm btn-info uib-datepicker-current" ng-click="select('today')" ng-disabled="isDisabled('today')">{{ getText('current') }}</button>
			<button type="button" class="btn btn-sm btn-danger uib-clear" ng-click="select(null)">{{ getText('clear') }}</button>
		</span>
        <button type="button" class="btn btn-sm btn-success pull-right uib-close" ng-click="close()">{{ getText('close') }}</button>
      </li>
    </ul>
  </script>

  <!-- Overriding code -->
</head>

<body>




  <div ng-controller="DatepickerDemoCtrl">



    <div class="row">
      <div class="col-xs-offset-3 col-xs-6 col-md-3">
        <h4 class="text-center">Date picker Popup</h4>
        <p class="input-group">
          <input type="text" class="form-control date-picker" uib-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"
          close-text="Close" />
          <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
        </p>
      </div>
    </div>

  </div>
</body>

</html>

CSS edit:

This is a non-standard way but it doesn't disturb your template structure anyway.

Plunker Demo

:before pseudo element in CSS supports content property to create text. Since you don't need to add anything other than header text/title, this is a fitting solution. Check the style.css file in the Plunkr to edit the text.

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

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

  // Disable weekend selection
  $scope.disabled = function(date, mode) {
    return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
  };

  $scope.toggleMin = function() {
    $scope.minDate = $scope.minDate ? null : new Date();
  };
  $scope.toggleMin();
  $scope.maxDate = new Date(2020, 5, 22);

  $scope.open = function($event) {
    $scope.status.opened = true;
  };

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

  $scope.dateOptions = {
    formatYear: 'yy',
    startingDay: 1
  };

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

  $scope.status = {
    opened: false
  };

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

  $scope.getDayClass = function(date, 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 '';
  };
});

.date-picker + .dropdown-menu::before {
  content: "When is your Birthday?";
  font-weight: bold;
  position: relative;
  left: 25%;
}
.date-picker + .dropdown-menu li {
  margin-top: 5px;
}

<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
  <script src="example.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  <link href="style.css" rel="stylesheet">
</head>

<body>

  <div ng-controller="DatepickerDemoCtrl">



    <div class="row">
      <div class="col-xs-offset-3 col-xs-6 col-sm-offset-4 col-sm-4 col-md-3">
        <h4 class="text-center">Date picker Popup</h4>
        <p class="input-group">
          <input type="text" class="form-control date-picker" uib-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"
          close-text="Close" />
          <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
        </p>
      </div>
    </div>

  </div>
</body>

</html>

这篇关于覆盖 AngularJs DatePicker 弹出窗口以添加新标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆