api地址
使用方式:
$('#id').data('daterangepicker').setStartDate(‘2018-12-11’);
$('#id').on('apply.daterangepicker', function (ev, picker) {
// code
// console.log("start = "+picker.startDate.format('YYYY-MM-DD')+" end = "+picker.endDate.format('YYYY-MM-DD'));
});
$('#id').off('.daterangepicker');
minDate: "", // 将会以当天为minDate
如果不需要minDate,则需要设置minDate: null,或者不设置
注意: startDate和endDate不能设置null,否则会报错
后来发现传null时typeof options.minDate === ‘object’,会执行下面的语句,导致左右切换箭头不显示,所以minDate如果没有的话,改为传false
if (typeof options.minDate === 'object')
this.minDate = moment(options.minDate);
$('#date').daterangepicker({
startDate: '2018-01-01',
endDate: '2018-12-10',
autoUpdateInput:false,
applyClass : 'btn-sm btn-success',
cancelClass : 'btn-sm btn-default',
locale: {
applyLabel: '确认',
cancelLabel: '取消',
fromLabel : '起始时间',
toLabel : '结束时间',
customRangeLabel : '自定义',
firstDay : 1,
format: 'YYYY-MM-DD',
},
opens : 'right', // 日期选择框的弹出位置
separator : ' 至 ',
"showDropdowns": true
}, function(start, end, label) { // 格式化日期显示框
//获取当前元素为 this.element
})
初始化日期弹窗的时候传入的回调函数,只会在用户点击了某个日期之后才会触发,假设用户选中默认日期,将不会触发
$('#date').daterangepicker({
startDate: '2018-01-01',
endDate: '2018-12-10',
autoUpdateInput:false,
applyClass : 'btn-sm btn-success',
cancelClass : 'btn-sm btn-default',
locale: {
applyLabel: '确认',
cancelLabel: '取消',
fromLabel : '起始时间',
toLabel : '结束时间',
customRangeLabel : '自定义',
firstDay : 1,
format: 'YYYY-MM-DD',
},
opens : 'right', // 日期选择框的弹出位置
separator : ' 至 ',
"showDropdowns": true
}, function(start, end, label) { // 格式化日期显示框
//不要在这里给input框赋值,当用户使用我们给出的默认开始时间时,该回调不触发
})
解决方式:
监听apply.daterangepicker事件
$('#date').on('apply.daterangepicker', function (ev, picker) {
// 这里填充数据
console.log(picker.startDate.format('YYYY-MM-DD'));
});
用this.element
$(obj).daterangepicker({
autoUpdateInput:false,
clearBtn:true,
"singleDatePicker": true,
"showDropdowns": true,
locale : {
format: 'YYYY-MM-DD',
}
},function(start, end, label) {
this.element.val(start.format('YYYY-MM-DD'))
if (is_clear) {
this.element.after('')
this.element.nextAll('.date_img').hide()
this.element.nextAll('.clear_time').show()
}
});
参考简书地址
添加参数clip,当定位元件在某个方向上溢出窗口时,将元素翻转到目标的另一侧,再次运行碰撞检测以查看它是否合适
this.flip = false; // 当定位元件在某个方向上溢出窗口时,将元素翻转到目标的另一侧,再次运行碰撞检测以查看它是否合适
if (typeof options.flip === 'boolean')
this.flip = options.flip;
move: function里
move: function() {
var parentOffset = { top: 0, left: 0 },
containerTop;
var parentRightEdge = $(window).width();
if (!this.parentEl.is('body')) {
parentOffset = {
top: this.parentEl.offset().top - this.parentEl.scrollTop(),
left: this.parentEl.offset().left - this.parentEl.scrollLeft()
};
parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
}
if (this.drops == 'up')
containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
else
containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
this.container[this.drops == 'up' ? 'addClass' : 'removeClass']('dropup');
/*************************** new add start ***********************/
if (this.flip && this.parentEl.is('body')) {
// 判断元素是否与窗口边缘发生碰撞,放不下
var dropUpFlag = this.drops == 'up' ? true : false;
if (this.drops == 'up') {
if (containerTop < 0) {
var tempTopDown = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
if (tempTopDown + this.container.outerHeight() < $('body').height()) {
containerTop = tempTopDown;
dropUpFlag = false;
} else {
containerTop = 0;
}
}
} else {
if (containerTop + this.container.outerHeight() > $('body').height()) {
var tempTopUp = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
if (tempTopUp >= 0) {
containerTop = tempTopUp;
dropUpFlag = true;
} else {
containerTop = $('body').height() - this.container.outerHeight();
}
}
}
this.container[dropUpFlag ? 'addClass' : 'removeClass']('dropup');
}
/*************************** new add end***********************/
var m_num= this.container.find('.single').length;
var isEmpty_customPickers = $.isEmptyObject(this.customPickers);
if(m_num==1){
if (!isEmpty_customPickers) {
this.container.css({
width:440
});
} else {
this.container.css({
width:350
});
}
}else{
if (!isEmpty_customPickers) {
this.container.css({
width:800
});
} else {
this.container.css({
width:710
});
}
}
if (this.opens == 'left') {
this.container.css({
top: containerTop,
right: parentRightEdge - this.element.offset().left - this.element.outerWidth(),
left: 'auto'
});
if (this.container.offset().left < 0) {
this.container.css({
right: 'auto',
left: 9
});
}
} else if (this.opens == 'center') {
this.container.css({
top: containerTop,
left: this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
- this.container.outerWidth() / 2,
right: 'auto'
});
if (this.container.offset().left < 0) {
this.container.css({
right: 'auto',
left: 9
});
}
} else {
this.container.css({
top: containerTop,
left: this.element.offset().left - parentOffset.left,
right: 'auto'
});
if (this.container.offset().left + this.container.outerWidth() > $(window).width()) {
this.container.css({
left: 'auto',
right: 0
});
}
}
},