【问题标题】:How to use a template in Angular UI Bootstrap Datepicker?如何在 Angular UI Bootstrap Datepicker 中使用模板?
【发布时间】:2021-10-14 13:36:38
【问题描述】:

In this question 解释了如何在 Angular UI Boostrap 中使用模板。但它没有 DatePicker 模板的示例。我需要更改字体并摆脱数字边框。我分析了 Angular UI javascript,但找不到模板。有什么想法吗?

这是PLUNK。

HTML

    <p class="input-group" style="width:200px;">
      <input type="text" class="form-control" is-open="true" ng-model="dt"
           uib-datepicker-popup="MM-dd-yyyy" 
           datepicker-options="dateOptions" close-text="Close"
           popup-placement="bottom-left" on-open-focus="false"/>
      <span class="input-group-btn">
          <button type="button" class="btn btn-default">
              <i class="glyphicon glyphicon-calendar"></i>
          </button>
      </span>
   </p>

【问题讨论】:

    标签: angularjs angular-ui-bootstrap angular-ui


    【解决方案1】:

    datepicker.html1

    <div class="uib-datepicker" ng-switch="datepickerMode" role="application" ng-keydown="keydown($event)">
      <uib-daypicker ng-switch-when="day" tabindex="0"></uib-daypicker>
      <uib-monthpicker ng-switch-when="month" tabindex="0"></uib-monthpicker>
      <uib-yearpicker ng-switch-when="year" tabindex="0"></uib-yearpicker>
    </div>
    

    daypicker 模板位于https://github.com/angular-ui/bootstrap/tree/master/template/datepicker


    试图改变天的背景颜色

    添加了内联样式

    在day.html 中,我将style="background-color:orange" 添加到包含日期的td。看看那个笨蛋。

    要将日期按钮背景更改为橙色,请添加以下 CSS:

    example.css

    .uib-day .btn-default { 
        background-color: orange;
    }
     .uib-day .btn-default:hover { 
        background-color: white;
    }
    

    这会覆盖uib-day 按钮的btn-default 背景。

    DEMO on PLNKR。

    【讨论】:

    • 是的,我知道,我尝试在 plunk 中使用它们,但它不起作用
    • 我添加了一个内联样式
    • 在day.html 中,我将style="background-color:orange" 添加到包含日期的td。看看笨蛋。
    • 如果您要覆盖模板,请确保使用没有模板的 ui-bootstrap 版本,然后从 repo 中获取您需要的模板并自己修改它们。简单的内联 CSS 是行不通的。
    • 我尝试了 plunker。 day.html 覆盖不起作用。橙色来自css,而不是day.html中的内联样式
    【解决方案2】:

    模板在这里:https://github.com/angular-ui/bootstrap/tree/master/template

    弹窗模板:https://github.com/angular-ui/bootstrap/tree/master/template/datepickerPopup

    这就是你使用它的方式:

    <input type="text" uib-datepicker-popup="MM/dd/yyyy" template-url="path/to/template.html" />
    

    【讨论】:

    【解决方案3】:

    正确的格式是

    <input type="text" class="form-control" name="dob"
    id="dob" datepicker-popup-template-url="views/partials/date-picker.html"
    uib-datepicker-popup="{{format}}" ng-model="dob" ng-required="false"
    show-weeks="false" datepicker-options="dateOptions" close-text="Close">
    

    您可以加载内联模板或外部模板

    它也可以在文档https://angular-ui.github.io/bootstrap/中找到

    【讨论】:

      【解决方案4】:

      这种方法对我有用。 首先将 popup.html,datepicker.html,day.html,month.html,year.html 文件复制到您的项目中。假设我已经在 datepicker 文件夹和 datepickerPopup 文件夹

      下添加了这些

      日期选择器文件夹

      • datepicker.html
      • day.html
      • 月.html
      • 年份.html

      datepickerPopup 文件夹

      • popup.html

      接下来,您必须为输入元素同时添加 datepicker-template-url 和 datepicker-popup-template-url

      <input type="text" class="form-control" 
           uib-datepicker-popup
           datepicker-template-url="libs/js/uib/template/datepicker/datepicker.html"
           datepicker-popup-template-url="libs/js/uib/template/datepickerPopup/popup.html"
           ng-keypress="$event.preventDefault()"
       />
      

      此外,如果您需要为日、月和年选择器添加自定义 HTML,您必须在 datepicker.html 中为每个选择器添加 template-url

      <div ng-switch="datepickerMode">
          <div template-url="libs/js/uib/template/datepicker/day.html" uib-daypicker ng-switch-when="day" tabindex="0" class="uib-daypicker">
          </div>
          <div template-url="libs/js/uib/template/datepicker/month.html" uib-monthpicker ng-switch-when="month" tabindex="0" class="uib-monthpicker"></div>
          <div template-url="libs/js/uib/template/datepicker/year.html" uib-yearpicker ng-switch-when="year" tabindex="0" class="uib-yearpicker"></div>
      </div>
      

      然后您可以自定义月、日或年 HTML 文件,这些更改将显示在 UI 中。例如,我在 day.html 中更改了月份更改器

      <table role="grid" aria-labelledby="{{::uniqueId}}-title" aria-activedescendant="{{activeDateId}}">
              <thead>
              <tr>
                  <th>
                      <button type="button" class="btn btn-default btn-sm pull-left uib-left" ng-click="move(-1)" tabindex="-1"><i
                              aria-hidden="true" class="fa fa-chevron-left"></i><span class="sr-only">previous</span></button>
                  </th>
                  <th colspan="{{::5 + showWeeks}}">
                      <button id="{{::uniqueId}}-title" role="heading" aria-live="assertive" aria-atomic="true" type="button"
                              style="background: #2a7abf;color: white;"
                              class="btn btn-default btn-sm uib-title" ng-click="toggleMode()"
                              ng-disabled="datepickerMode === maxMode" tabindex="-1">
                          <strong style="display: flex;justify-content: center;">{{title}}
                              <i style="margin-left: 10px;font-size: 18px;" class="fa fa-caret-down" aria-hidden="true"></i>
                          </strong>
                      </button>
                  </th>
                  <th>
                      <button type="button" class="btn btn-default btn-sm pull-right uib-right" ng-click="move(1)" tabindex="-1">
                          <i aria-hidden="true" class="fa fa-chevron-right"></i><span class="sr-only">next</span></button>
                  </th>
              </tr>
              <tr>
                  <th ng-if="showWeeks" class="text-center"></th>
                  <th ng-repeat="label in ::labels track by $index" class="text-center"><small aria-label="{{::label.full}}">{{::label.abbr}}</small>
                  </th>
              </tr>
              </thead>
              <tbody>
              <tr class="uib-weeks" ng-repeat="row in rows track by $index" role="row">
                  <td ng-if="showWeeks" class="text-center h6"><em>{{ weekNumbers[$index] }}</em></td>
                  <td ng-repeat="dt in row" class="uib-day text-center" role="gridcell"
                      id="{{::dt.uid}}"
                      ng-class="::dt.customClass">
                      <button type="button" class="btn btn-default btn-sm"
                              uib-is-class="
                      'btn-info' for selectedDt,
                      'active' for activeDt
                      on dt"
                              ng-click="select(dt.date)"
                              ng-disabled="::dt.disabled"
                              tabindex="-1"><span ng-class="::{'text-muted': dt.secondary, 'text-info': dt.current}">{{::dt.label}}</span>
                      </button>
                  </td>
              </tr>
              </tbody>
          </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多