Angular bootstrap date-range-picker in a popup?

2020-06-17 06:46发布

问题:

I am trying to show a date-range picker in my angular app using ng-bootstrap. My code looks like following:

<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>

How should I show the above range picker in a pop-up?

As per an answer, I tried the following but the popup is not visible on click:

 <!-- 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>

回答1:

To display your date-range-picker in a Bootstrap 4 popup/modal, you can use the following HTML as a template (click the "run code snippet" button below for a live test).

Important for ng-bootstrap users:

The popper.js is required in Bootstrap 4 for all things that pop up or drop down. So, make sure you load that in your application.

<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>

Reference:

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



回答2:

If you don't want to use a full-fledged popup, but rather show the date range picker in a flyout, I made a simple demo using ng-bootstrap and moment.js: https://stackblitz.com/edit/ng-bootstrap-date-range

It heavily borrows from the official range selection demo provided by ng-bootstrap.



回答3:

Thanks for explained reply and solution, yes while we are using only bootstrap 4 then we might need the dependencies stated over. But these are not needed while we are working with ng-bootstrap, I got it from the official documentation of Ng-bootstrap .

Please add your valued comments to it, I am new to learning Angular and its extensions, please clear my doubt too.