我正在尝试使用动态创建的数据来实现popover in bootstrap datepicker。我每天都有不同的价格,并希望显示为popover。
到目前为止,我能够从datepicker的beforeShowDay
函数返回每个价格,但由于它不是全局变量,因此无法在popover中访问此数据。这就是我的意思;
日期选择器;
var checkin = $('#from').datepicker({
format: 'dd/mm/yyyy',
startDate: '+1d',
weekStart: 1,
language: "<%= params[:locale] %>",
beforeShowDay: function (date) {
var ahora = new Date();
if (date >= ahora.setHours(0,0,0,0) ) {
var date_real = date.getFullYear() + '-' + ( '0' + (date.getMonth()+1) ).slice( -2 ) + '-' + ( '0' + (date.getDate()) ).slice( -2 )
for(var l = 0; l < length; l++){
//console.log(date_real)
//console.log(schedules[l].date)
var pop_price;
if(schedules[l].date === date_real && schedules[l].price !== ""){
//console.log(schedules[l].price);
pop_price = schedules[l].price;
break;
}
else{
// console.log(basePrice);
pop_price = basePrice;
}
}
console.log(pop_price); //here I can get each day's price, either base price or custom price.
};
//finishes here
dmy = date.getDate() + "/" + (date.getMonth()+1) + "/" + date.getFullYear();
if ($.inArray(dmy, closed_formatted) == -1) {
return true;
} else {
return false;
}
},
autoclose: true
}).on('changeDate', function (ev) {
});
然后是popover;
$('body').on({
mouseenter: function() {
$(this).popover({
placement: 'top',
container: '.datepicker',
content: 'Demo', // here I have to put pop_price
}).popover('show');
},
mouseleave: function() {
$(this).popover('hide')
}
}, '.datepicker .day:not(.disabled)');
所以在content
而不是编写demo时,我必须每天都显示pop_price变量。