DateRangePicker是一款时间范围选择器,界面良好,很是适合短期范围选择的插件,具体源码能够在http://www.daterangepicker.com/找到app
,可是目前使用中,感受功能不甚齐全,因此将遇到的问题和解决方法记录下来。函数
1没法默认值为空this
DateRangePicker能够经过在初始化时设置startDate和endDate属性,来实现设置初始默认值。其中能够引用moment这一款专门针对date的时间管理插件,例如使用options.startDate=moment() 设置初始默认值为当前时间spa
可是当想设置初始默认值为空时,即便设置startDate和endDate为 ' '也不行,会提示日期格式不对,没法实现。插件
因此,在此咱们可使用autoUpdateInput属性,autoUpdateInput是用来打开和关闭daterangepicker选择时,是否自动传值到input[text] 这个DOM的属性,经过设置初始autoUpdateInput为false,能够实现初始值为空,这是在input中设置的placeholder才能正常显现出来。事件
可是设置该属性以后,无论怎么选择daterangePikcer的日期,都不会有传值到input中,也就是没有办法正常显示选择的日期了,因此要在恰当的时刻,调用$(id).data('daterangepicker').autoUpdateInput=true,就能够了。ci
做者最初设置为,最初默认值为空,当daterangepicker 的input发生点击时,autoUpadateInput=true,可是这时出现input无论是否选中日期,都会自动有值,因此为了修改这个问题,我在daterangepicker的源码中进行了修改,固然也能够从新更改须要的onclick事件。input
在源码中,当autoUpdateInput设置为false以后,咱们想要在点击肯定,选中日期和点击range三个地方,将autoUpdateInput改变回来,因此,在三处设置了this.autoUpdateInput=true属性,源码
在io
在1207行左右,clickrange函数中,当点击选择了range时,改变autoUpdateInput属性,在else中加入,是由于当选择自定义标签时,若是没有选择日期,依旧不改变默认值。选择日期的改变,在后文所示。
} else if (!this.endDate && date.isBefore(this.startDate)) {
this.autoUpdateInput=true;
//special case: clicking the same date for start/end,
//but the time of the end date is before the start date
this.setEndDate(this.startDate.clone());
} else { // picking end
this.autoUpdateInput=true;
if (this.timePicker) {
在1335行左右,clickdate函数中,当点击选择了enddate也就是完整的选择了日期时
} else if (!this.endDate && date.isBefore(this.startDate)) {
this.autoUpdateInput=true;
//special case: clicking the same date for start/end,
//but the time of the end date is before the start date
this.setEndDate(this.startDate.clone());
} else { // picking end
this.autoUpdateInput=true;
if (this.timePicker) {
在1400行左右,还有一种状况,就是当有时分秒的时间显示时,有可能用户会直接点击确认,或更改时分秒,来选择日期,因此在clickapply函数中,也改变autoUpdateInput值
clickApply: function(e) {
this.autoUpdateInput=true;