9

I want to use date-picker popup in input control using angular 2, when do blur on input control, then date-picker pop should come like bootstrap datepicker, I want to do it in angular 2, please suggest me any reference of date-picker of Angular 2.

br.julien
  • 3,420
  • 2
  • 23
  • 44
asad mohd
  • 181
  • 1
  • 1
  • 7

7 Answers7

25

I created my own datepicker-popup based on the ng2-bootstrap datepicker.

HTML

<my-datepicker [dateModel]="endDate" [label]="'Startdatum'" ></my-datepicker>

Angular2-Component in TypeScript:

import {Component, Input, Output, EventEmitter} from '@angular/core';
import {DATEPICKER_DIRECTIVES} from 'ng2-bootstrap/components/datepicker';

@Component({
    selector: 'my-datepicker',
    directives: [DATEPICKER_DIRECTIVES],
    template: `
      <label>{{label}}</label>
      <input [(ngModel)]="dateModel" class="form-control"     (focus)="showPopup()" />
      <datepicker class="popup" *ngIf="showDatepicker" [(ngModel)]="dateModel" [showWeeks]="true" (ngModelChange)="hidePopup($event)" ></datepicker>
  `,
    styles: [`
    .popup {
      position: absolute;
      background-color: #fff;
      border-radius: 3px;
      border: 1px solid #ddd;
      height: 251px;
    }
  `],
})
export class IsdDatepickerComponent {
    @Input()
    dateModel: Date;
    @Input()
    label: string;
    @Output()
    dateModelChange: EventEmitter<string> = new EventEmitter();
    private showDatepicker: boolean = false;

    showPopup() {
        this.showDatepicker = true;
    }

    hidePopup(event) {
        this.showDatepicker = false;
        this.dateModel = event;
        this.dateModelChange.emit(event)
    }
}
Christoph
  • 1,993
  • 1
  • 25
  • 33
  • Solid control, @Christoph. – tylerjgarland Jul 21 '16 at 01:07
  • Perfect! Thanks for sharing – Abu Abdullah Sep 17 '16 at 08:02
  • Could you please update the showWeeks attribute (it should be wrapped in [] brackets). Stackoverflow doesn't allow edits less than 6 characters :@ – Abu Abdullah Sep 17 '16 at 08:12
  • 1
    @AbuAbdullah Done. – Christoph Sep 19 '16 at 10:56
  • @Christoph -- Superb ! .. Is there any way to hide the popup only when user click the date. Using the current code popup is closing when user click on date/month/year. – Nidhin T T Sep 30 '16 at 07:02
  • I have another issue - after the date is filled, the datepicker is not shown anymore, even though showDatepicker is correctly set to true via showPopup ... If I delete the input content it shows again ... any ideas? – markz Nov 08 '16 at 14:01
  • Ok, I got it, if anybody else will have this problem - the click on the input field triggers a ngModelChange event which closes the popup. I had to compare the value in the input field with the previously set value and return hidePopup when there was no change – markz Nov 08 '16 at 14:27
  • @NidhinTT call hidePopup method on selectionDone event instead of modal change to do that – Shyam Shinde Nov 11 '16 at 13:43
  • When i change months or years the popup disapear i think you have to some contrtole to ngModelChange to check if on day is changed before to hide the popup – Dahar Youssef Dec 05 '16 at 10:36
  • @ShyamShinde - Thankyou so much for your comment - I have been struggling with this for hours now! – user3640967 Jan 18 '17 at 12:09
  • 1
    ng2-bootstrap currently doesn't ship with components. I'm missing something – trees_are_great Mar 06 '17 at 11:35
3

Check out this date picker, it is highly configurable and its only dependency is moment.
https://github.com/vlio20/ng2-date-picker

vlio20
  • 8,955
  • 18
  • 95
  • 180
  • The other one seemed buggy and this seems to work better, although it will require some css changes I think – trees_are_great Mar 06 '17 at 13:57
  • Note that there is a material style class. And the css was built to be easy to style as you wish – vlio20 Mar 06 '17 at 14:33
  • I am struggling to find this css - I can see a less file, but it seems to be empty. Please let me know my my ignorance :) – trees_are_great Mar 06 '17 at 17:13
  • Please refer to the documentation. You will find it there. Did you see the demo? – vlio20 Mar 06 '17 at 17:14
  • I can see in the css page of the demo, there are styles, such as: ".dp-calendar-nav-left[_ngcontent-ppw-0], .dp-calendar-nav-right[_ngcontent-ppw-0]{" which have been generated by css in an angular component. I guess I could just copy this css, remove the square brackets with their content from each line and then use this? – trees_are_great Mar 06 '17 at 17:23
  • You can just overwrite them as an ordinary element – vlio20 Mar 06 '17 at 18:30
2

Just use selectionDone inplace of ngModelChange event. I used in my code:

    import {Component, Input, Output, EventEmitter} from '@angular/core';
import {DATEPICKER_DIRECTIVES} from 'ng2-bootstrap/components/datepicker';

@Component({
    selector: 'my-datepicker',
    directives: [DATEPICKER_DIRECTIVES],
    template: `
      <label>{{label}}</label>
      <input [(ngModel)]="dateModel" class="form-control"     (focus)="showPopup()" />
      <datepicker class="popup" *ngIf="showDatepicker" [(ngModel)]="dateModel" [showWeeks]="true" (selectionDone)="hidePopup($event)" ></datepicker>
  `,
    styles: [`
    .popup {
      position: absolute;
      background-color: #fff;
      border-radius: 3px;
      border: 1px solid #ddd;
      height: 251px;
    }
  `],
})
export class IsdDatepickerComponent {
    @Input()
    dateModel: Date;
    @Input()
    label: string;
    @Output()
    dateModelChange: EventEmitter<string> = new EventEmitter();
    private showDatepicker: boolean = false;

    showPopup() {
        this.showDatepicker = true;
    }

    hidePopup(event) {
        this.showDatepicker = false;
        this.dateModel = event;
        this.dateModelChange.emit(event)
    }
}
1

There is a new, excellent implementation of datepicker for Angular 2 based on Bootstrap 4: https://ng-bootstrap.github.io/#/components/datepicker.

It is a fully native widget with forms integration and very flexible cusomization options.

pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286
  • The only thing with that is, it's for bootstrap 4. I also noticed that when you click outside the datepicker, it does not automatically close the datepicker. Though I keep an eye on it, looks very interesting, next to ng2-bootstrap (has an bootstrap3/4 version). – Anna Smother Oct 25 '16 at 07:08
  • Bootstrap 4 is still alpha, while I look forward to it being released this spring I wouldn't recommend it for most projects. – Scott White Jan 10 '17 at 05:36
1

this is my implementation based on Christophs answer. i am using two way binding. component emits date as a string yyyy-mm-dd for saving on server. but it shows date inside component as a string dd-mm-yyyy . its convinient for cross browser displaying

 <datepicker-popup [(dateModel)]="serverDate"></datepicker-popup>

ts:

import {Component, Input, Output, EventEmitter, OnInit} from '@angular/core';
import {DatePickerComponent} from 'ng2-bootstrap/datepicker';

@Component({
selector: 'datepicker-popup',
template: `
  <input type="text" [(ngModel)]="_dateModel" class="form-control" (click)="openPopup()">
  <datepicker class="popup"
      [hidden]="!showPopup"
      [(ngModel)]="dateModelObj"
      [showWeeks]="true"
      (ngModelChange)="closePopup($event)" >
  </datepicker>


 `,
    styles: [`
    .popup {
      position: absolute;
      background-color: #fff;
      border-radius: 3px;
      border: 1px solid #ddd;
      height: 251px;
    }
  `],
})
export class DatepickerPopupComponent implements OnInit{
    @Input() dateModel: string;
    @Output()  dateModelChange: EventEmitter<string> = new EventEmitter();
    showPopup: boolean = false;
    _dateModel: string;
    dateModelObj: any;

ngOnInit() {
    this.dateModelObj = new Date(this.dateModel)
}

openPopup() {
    this.showPopup = true;
}

closePopup(event) {
    this.showPopup = false;
    this._dateModel = this.DDMMYYYY(event);
    this.dateModelChange.emit(this.YYYYMMDD(event))
}

YYYYMMDD(date):string {
    if (!date) return null;
    return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
};

DDMMYYYY(date):string {
    if (!date) return null;
    return date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
};

}

dimson d
  • 1,488
  • 1
  • 13
  • 14
1

Try this, Pre requirments ng2-bootstrap

import date picker module in your app module

import { DatepickerModule } from 'ng2-bootstrap';

app-datepicker

<app-datepicker name="date" [(ngModel)]="driver.dateOfBirth" [placeholder]="'Date of Birth'" [dateFormat]="'dd-MM-yyyy'" ngDefaultControl required></app-datepicker>

datepicker.component.ts

import { Component, ViewChild, Input, Output, EventEmitter, ElementRef, Renderer } from '@angular/core';
import { DatePipe } from '@angular/common';

@Component({
    selector: 'app-datepicker',
    templateUrl: './datepicker.component.html',
    styleUrls: ['./datepicker.component.css']
})

    export class DatepickerComponent {
        public inputHeight: String;

        @ViewChild('tasknote') input: ElementRef;
        @Input()
        ngModel: String;
        @Input()
        label: string;
        @Input()
        dateFormat: string;
        @Input()
        placeholder: string;
        @Output()
        ngModelChange: EventEmitter<string> = new EventEmitter();

        private showDatepicker: boolean = false;

        constructor(public element: ElementRef) {
        }

        showPopup(element) {
            this.inputHeight = this.element.nativeElement.querySelector('input').getBoundingClientRect().height + 'px';
            this.showDatepicker = true;
        }

        hidePopup(event) {
            this.showDatepicker = false;
            if (event) {
                var date = new DatePipe('en-us').transform(event, this.dateFormat || 'dd/MM/yyyy');
                this.ngModel = date;
                this.ngModelChange.emit(date);
            }
        }
    }

datepicker.component.html

<div style="position: relative;">
    <label [ngClass]="{'hide': !label}">{{label}}</label>
    <input #tasknote name="date-picker" [(ngModel)]="ngModel" (focus)="showPopup($elem)" placeholder="{{placeholder}}" required/>
    <datepicker class="popup" [ngClass]="{'hide': !showDatepicker}" [(ngModel)]="date" [showWeeks]="true" (selectionDone)="hidePopup($event)"
        [ngStyle]="{'bottom': inputHeight}"></datepicker>
</div>

datepicker.component.css

.popup {
      position: absolute;
      background-color: #fff;
      border-radius: 3px;
      border: 1px solid #ddd;
      height: 251px;
      color: #000;
      right: 0px;
    }
.hide {
     display:none;
}
byteC0de
  • 5,153
  • 5
  • 33
  • 66
-7

You could use the ng2-boostrap project that provides a date picker component:

<datepicker [(ngModel)]="date" showWeeks="true"></datepicker>

See the project page:

See this question for the way to configure ng2-bootstrap (and the moment library) within the SystemJS configuration:

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360