How to add pop up date pickar using ngX-bootstrap?

Deadly 提交于 2019-12-13 08:19:10

问题


How to add datepickar using ngx-bootstrap? This project uses Angular2 typescript2 ngx-bootstrap. I am doing following things: In HTML :

<datepicker class="well well-sm main-calendar" [(ngModel)]="dt" [minDate]="minDate" [showWeeks]="false" [dateDisabled]="dateDisabled"></datepicker>

In Module:

import { DatepickerModule } from 'ngx-bootstrap/datepicker';
@NgModule({
    imports: [
        DatepickerModule.forRoot() 
    ]});

回答1:


The feature does not currently exist in the library. There is a discussion on it here, but nothing has come so far:

https://github.com/valor-software/ngx-bootstrap/issues/273

I created a workaround for it using the bootstrap dropdown with the datepicker. Here is the HTML (please note that MinDate and dateValue are both values inside my Component class):

<div class="dropdown" dropdown #datepickerDropdown="bs-dropdown" [autoClose]="false">
  <div class="input-group" dropdownToggle>
    <input type="text" disabled="disabled" class="form-control" [value]="dateValue | date:'shortDate'" />
    <div class="input-group-addon">
      <span class="glyphicon glyphicon-calendar"></span>
    </div>
  </div>
  <div *dropdownMenu class="dropdown-menu dropdown-date">
    <datepicker [(ngModel)]="dateValue" [minDate]="MinDate" [showWeeks]="false" (selectionDone)="closeDropdown()"></datepicker>
  </div>
</div>

Inside your module, make sure to include:

import { DatepickerModule } from 'ngx-bootstrap/datepicker';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';

and don't forget

@NgModule({
  declarations: [...],
  imports: [
    BsDropdownModule.forRoot(),
    DatepickerModule.forRoot(),
    ... (any other imports)
  ],
  providers: [...],
  bootstrap: [AppComponent]
])

In order to auto-close the dropdown on date selection, I didn't want it to autoclose when clicking in case the user wanted to change months. Therefore we had to use the selectionDate event. To create the function properly, we'll need to pull this into the Component. You will need to include the following imports in the Component:

import { ViewChild, ... } from '@angular/core';
import { BsDropdownDirective } from 'ngx-bootstrap/dropdown';

You can then reference the dropdown inside the Component class by doing the following:

@ViewChild('datepickerDropdown') private datepickerDropdown: BsDropdownDirective;

closeDropdown() {
  this.datepickerDropdown.hide();
}

I hope I was able to help. This works for me, but I'm not sure if it will work for everybody else.



来源:https://stackoverflow.com/questions/43650171/how-to-add-pop-up-date-pickar-using-ngx-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!