Skip to content

Light and simple date range picker component for Angular2 based on Material

License

Notifications You must be signed in to change notification settings

albertnadal/ng2-daterange-picker

Repository files navigation

Angular 2 Material Daterange picker

Version

A minimalist daterange picker component for Angular 2 based on Material. No jQuery dependent. No Bootstrap CSS dependent.

Installation

npm install ng2-daterange-picker --save

Usage

Import the DaterangePicker Module and add it to the imports of your module

import { DaterangePickerModule } from 'ng2-daterange-picker'

@NgModule({
  imports: [ DaterangePickerModule ],
  declarations: [ ... ],
  bootstrap: [ ... ]
})
export class YourModule { }

Use dinamically the ng2-daterange-picker selector as described in the sample component below

import { Component, ViewContainerRef, ViewChild, ComponentFactoryResolver } from '@angular/core';
import { DaterangePickerComponent } from 'ng2-daterange-picker';

@Component({
  selector: 'my-component-selector',
  entryComponents: [DaterangePickerComponent],
  template: '<button #daterangePicker type="button" (click)="showDaterangePickerSelector()">Select a range</button>'
})
export class MyComponent {

  @ViewChild('daterangePicker', { read: ViewContainerRef }) daterangePickerParentViewContainer: ViewContainerRef;

  constructor(private componentFactory: ComponentFactoryResolver) { }

  showDaterangePickerSelector() {
    let daterangePickerComponentFactory = this.componentFactory.resolveComponentFactory(DaterangePickerComponent);
    let daterangePickerComponent: DaterangePickerComponent = DaterangePickerComponent.initWithData(this.daterangePickerParentViewContainer, daterangePickerComponentFactory);

    daterangePickerComponent.onSelectedDaterange.subscribe(
          data => {
                    this.showSelectedDaterange(data.startDate, data.endDate);
          }
    );
  }

  showSelectedDaterange(startDate: Date, endDate: Date) {
    console.log(startDate);
    console.log(endDate);
  }

}

License

MIT