【问题标题】:Angular bootstrap date-range-picker in a popup?弹出窗口中的角度引导日期范围选择器?
【发布时间】:2018-07-22 23:28:22
【问题描述】:

我正在尝试使用 ng-bootstrap 在我的 Angular 应用程序中显示日期范围选择器。我的代码如下所示:

<ngb-datepicker #dp ngModel (ngModelChange)="onDateChange($event)" [displayMonths]="2" [dayTemplate]="t">
            </ngb-datepicker>

            <ng-template #t let-date="date" let-focused="focused">
              <span class="custom-day"
                    [class.focused]="focused"
                    [class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
                    [class.faded]="isHovered(date) || isInside(date)"
                    (mouseenter)="hoveredDate = date"
                    (mouseleave)="hoveredDate = null">
                {{ date.day }}
              </span>
            </ng-template>

我应该如何在弹出窗口中显示上述范围选择器?

根据答案,我尝试了以下操作,但点击时看不到弹出窗口:

 <!-- Button trigger modal -->
          <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
            Pick Date Range
           </button>

    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Optional title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">

            <ngb-datepicker #dp ngModel (ngModelChange)="onDateChange($event)" [displayMonths]="2" [dayTemplate]="t">
            </ngb-datepicker>

            <ng-template #t let-date="date" let-focused="focused">
              <span class="custom-day"
                    [class.focused]="focused"
                    [class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
                    [class.faded]="isHovered(date) || isInside(date)"
                    (mouseenter)="hoveredDate = date"
                    (mouseleave)="hoveredDate = null">
                {{ date.day }}
              </span>
            </ng-template>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

【问题讨论】:

    标签: angular popup bootstrap-4 ng-bootstrap


    【解决方案1】:

    如果您不想使用完整的弹出窗口,而是在弹出窗口中显示日期范围选择器,我使用 做了一个简单的演示:https://stackblitz.com/edit/ng-bootstrap-date-range

    它大量借鉴了 ng-bootstrap 提供的官方range selection demo

    【讨论】:

    • 感谢 Angular 2+ 上此功能的唯一工作示例
    • 完美使用 ngb-datepicker,干得漂亮。和这里最好的例子,这应该是被接受的答案。
    【解决方案2】:

    要在 Bootstrap 4 弹出窗口/模式中显示您的日期范围选择器,您可以使用以下 HTML 作为模板(单击下面的“运行代码 sn-p”按钮进行实时测试)。

    ng-bootstrap 用户很重要:

    popper.js 是 Bootstrap 4 中所有弹出或下拉内容所必需的。因此,请确保将其加载到您的应用程序中。

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
    
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Click this button!
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Optional title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            Your date-range-picker code goes here...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

    参考:

    https://getbootstrap.com/docs/4.0/components/modal/

    【讨论】:

    • 感谢您的回答。我尝试像您在示例中那样将日期选择器放在模态主体 div 中,但是单击时我看不到 div。
    • popper.js 是您在 Bootstrap 4 中弹出或下拉的所有内容的朋友。确保加载它。查看我的代码 sn-p。
    • 这就解释了。我会尝试把它。感谢您对此的指导。
    • 或者,您可以加载 bootstrap.bundle.min.js,因为其中包括 popper.js
    【解决方案3】:

    感谢您解释的回复和解决方案,是的,虽然我们只使用 bootstrap 4,但我们可能需要上述依赖项。但是在我们使用 ng-bootstrap 时这些都不需要,我是从Ng-bootstrap 的官方文档中得到的。

    请添加您宝贵的 cmets,我是学习 Angular 及其扩展的新手,也请清除我的疑问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多