Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open and hide ng-bootstrap datepicker on custom actions?

Currently I am using:

  • ng-bootstrap 1.0.0-alpha.24
  • angular/core 4.0.0
  • bootstrap 4.0.0-alpha.6

I wanted to ask if someone knows how to autoclose the datepicker when the focus is lost or another datepicker is opened.

Also i wanted to now if it is possible to close the datepicker in the component code with typescript.

It would be nice if someone could provide a working plunker or a code snippet.

My actual implementation:

<div class="input-group">
    <input class="rect-border full-width"
           placeholder="YYMMDD"
           [(ngModel)]="selectedDate"
           ngbDatepicker
           #datePickerInput="ngbDatepicker"
           (keydown.arrowup)="incrementDate()"
           (keydown.arrowdown)="decrementDate()"
           (ngModelChange)="validate('modelChanged')"
           (blur)="validate(null)"
           [disabled]="disabled"
           [ngClass]="{'input-required': required, 'normal-color': valid, 'picker-disabled': disabled}">

    <div class="input-group-addon rect-border"
         (click)="disabled ? true : datePickerInput.toggle()"
         [ngClass]="{'picker-button-disabled': disabled}">
        <img src="assets/img/calendar-icon.svg" class="datpickerToggle"/>
    </div>
</div>

Plunker: ng-bootstrap team demo

I have searched a long time and I am also pretty new to angular and these things.

Thank you for your help!

Update:

Possible solution:

There were a lot of good solutions provided. I also found out by myself that I could use the class NgbInputDatepicker to close the datePicker (I always used NgbDatepicker, so it didn't work).

@ViewChild('datePickerInput') datePicker: NgbInputDatepicker;

this.datePicker.close();
like image 838
WorstCoderEver Avatar asked May 08 '17 08:05

WorstCoderEver


3 Answers

you can open and close your datepicker from your html itself

for eg:

<div class="input-group">
    <input class="rect-border full-width"
           placeholder="YYMMDD"
           [(ngModel)]="selectedDate"
           ngbDatepicker
           #datePickerInput="ngbDatepicker"
           (keydown.arrowup)="incrementDate()"
           (keydown.arrowdown)="decrementDate()"
           (ngModelChange)="validate('modelChanged')"
           (blur)="validate(null)"
           [disabled]="disabled"
           [ngClass]="{'input-required': required, 'normal-color': valid, 'picker-disabled': disabled}">

    <div class="input-group-addon rect-border"
         (click)="disabled ? true : datePickerInput.toggle()"
         [ngClass]="{'picker-button-disabled': disabled}">
        <img src="assets/img/calendar-icon.svg" class="datpickerToggle"/>
    </div>
</div>

<div (click)="datePickerInput.open()"></div>

<span (click)="datePickerInput.close()"></span>

and also there are many functions which you can use in your html. some are close(), isOpen(), manualDateChange(), open(), toggle(), validate() etc. You can refer it in this plunkr http://plnkr.co/edit/G1b6fFrtVZwEz4lsou8n?p=preview

like image 193
sainu Avatar answered Nov 03 '22 12:11

sainu


In typescript you can simply define a variable datepickerVisibility and then in your template use *ngIf to show or hide your datepicker component. Here is a demo code:

Template: <datepicker *ngIf="datepickerVisibility" [ngModel]="date"> </datepicker>

Component: private datepickerVisibility: boolean = false; // Show the datepicker showDatepicker() { this.datepickerVisibility = true; }

Edit:

Therefore you could use jQuery. Add the jQuery js into your index.html and in your typescript component use jQuery as follows:

declare let jQuery: any;

@Component({
    moduleId: module.id,
    selector: 'test',
    templateUrl: 'template.html',
    styleUrls: ['test.css'],
})
export class TestComponent {
   constructor() {}

    public toggleDatepicker() {
        jQuery("#datepicker01").toggle();
   }
 }

And in your template file just add the id datepicker01 to your datepicker div

<div id="datepicker01" ...>

like image 2
Tom Mühlegger Avatar answered Nov 03 '22 11:11

Tom Mühlegger


I was looking for a solution to this issue, but in a scenario where the datepicker is wrapped in a custom component and has to expose a function a parent component can call to toggle the datepicker. The answers provided are great and will work for most use case, but not mine, as I didn't want to add a jQuery dependency and calling toggle from the HTML isn't an option.

Here's how I solved it.

I added a ViewChild reference to the datepicker input in the *.component.ts file

  @ViewChild('d', {static: true}) d;

that matches the datepicker identifier in the HTML file

<input (dateSelect)="dateSelected($event)" class="form-control" (focus)='d.toggle()' placeholder="yyyy-mm-dd" name="d"
  [ngModelOptions]="{standalone: true}" [(ngModel)]="date" ngbDatepicker #d="ngbDatepicker">

and called the toggle function within a method exposed by the component

toggle() {
  this.d.toggle();
}

That way, another component can call the toggle() function exposed by this component to toggle the datepicker like so:

In HTML

<app-custom-datepicker #date></app-custom-date-picker>

In .ts file

@ViewChild('date', {static: true}) date;
.
.
.
this.date.toggle();
like image 1
Ikenna Anthony Okafor Avatar answered Nov 03 '22 11:11

Ikenna Anthony Okafor