本文介绍了我如何在我的angular 2项目中使用daterangepicker js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在angular 2项目中工作,想集成daterangepicker.js库以弹出日期范围选择器。如果您未在

i am working in angular 2 project and want to integrate daterangepicker.js library to pop up date range picker. here link to library in case you do not hear about it before http://www.daterangepicker.com/

htmlcode:

<input type="text" placeholder="Start - End" class="oa-search-height padd" name="daterange" > </div>

并在我的index.html内

and inside my index.html

 <script src="./assets/js/daterangepicker.min.js" > </script>
<link rel="stylesheet" type="text/css" href ="./assets/css/daterangepicker-bs3.min.css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
 $('input[name="daterange"]').daterangepicker();
   });
    </script>

我也创建指令

  import { Directive,  ElementRef, Input, Renderer } from '@angular/Core';

  declare var jQuery: any;
  @Directive({
     selector: '[date-range]',
     host: {
          '(keyup)': 'dateRangePicker()'
           }
         })
  export class  DateRangeDirective {
      private element: HTMLElement;
      private renderer: Renderer;

      constructor(element: ElementRef, renderer: Renderer) {
             this.element = element.nativeElement;
             this.renderer = renderer;
                          }
             @Input('date-range');
             dateRangePicker(){
               jQuery(this.element).daterangepicker();
                               }

       }


推荐答案

可用的样本对,请参考以下选项-

Couple of samples available, refer these options-


看看是否有帮助。

编辑:



import { Component } from '@angular/core';
import { DateRangePickerDirective } from 'ng2-daterangepicker';

@Component({
  moduleId: module.id,
  selector: 'app',
  template: `
    <div class="container">
      <div class="jumbotron">
        <h2 class="text-center">Date Range Picker</h2>
        <div class="form-group">
          <input daterangepicker
                (selected)="dateSelected($event)"
                [options]="pickerOptions" type="submit"
                class="form-control btn btn-success"/>
        </div>
      </div>
    </div>
  `,
  directives: [DateRangePickerDirective],
  styles: [`
    .jumbotron {
      margin-top: 15px;
      box-shadow: 0px 3px 10px 3px rgba(0,0,0,0.2);
    }
    input { cursor: pointer; }
  `]
})

export class AppComponent {
  pickerOptions: Object = {
    'showDropdowns': true,
    'showWeekNumbers': true,
    'timePickerIncrement': 5,
    'autoApply': true,
    'startDate': '05/28/2016',
    'endDate': '06/03/2016'
  };

  dateSelected(message) {
    alert(message);
  }
}





import {Directive, ElementRef, OnInit, Input, Output, EventEmitter} from '@angular/core';

@Directive({
  selector: '[daterangepicker]'
})

export class DateRangePickerDirective implements OnInit {
  @Input() options: Object = {};
  @Output() selected: any = new EventEmitter();

  constructor(private elementRef: ElementRef) {}

  ngOnInit() {
    $(this.elementRef.nativeElement)
      .daterangepicker(this.options, this.dateCallback.bind(this));
  }

  dateCallback(start, end, label) {
    let message = `
      New date range selected:
      ${start.format('YYYY-MM-DD')} to ${end.format('YYYY-MM-DD')}
      (predefined range: ${label})
    `;
    this.selected.emit(message);
  }
}

这篇关于我如何在我的angular 2项目中使用daterangepicker js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 16:16