没有内置的解决方案可以做你想做的事,而且多天的事件不能跳过间隔内的几天;所以我喜欢找到一个,这是我的解决方案!
脚本是这样工作的;对于给定的开始日期,我根据周末找到正确的接下来两天,并使用单个日期设置三个变量。对于每个日期,我都会找到周数并使用它来查找是否必须创建多天事件或更多事件。
最相关的代码是:
var firstDay = new Date(date.getTime());
while (firstDay.getDay() == 0 || firstDay.getDay() == 6) {
firstDay.setDate(firstDay.getDate() + 1);
}
var secondDay = new Date(firstDay.getTime());
do {
secondDay.setDate(secondDay.getDate() + 1);
} while (secondDay.getDay() == 0 || secondDay.getDay() == 6);
var thirdDay = new Date(secondDay.getTime());
do {
thirdDay.setDate(thirdDay.getDate() + 1);
} while (thirdDay.getDay() == 0 || thirdDay.getDay() == 6);
检查日期的星期并检查星期日期是否相同,我使用以下功能:
function getWeekNr(today)
{
Year = takeYear(today);
Month = today.getMonth();
Day = today.getDate();
now = Date.UTC(Year,Month,Day+1,0,0,0);
var Firstday = new Date();
Firstday.setYear(Year);
Firstday.setMonth(0);
Firstday.setDate(1);
then = Date.UTC(Year,0,1,0,0,0);
var Compensation = Firstday.getDay();
if (Compensation > 3) Compensation -= 4;
else Compensation += 3;
NumberOfWeek = Math.round((((now-then)/86400000)+Compensation)/7);
return NumberOfWeek;
}
function takeYear(theDate)
{
x = theDate.getYear();
var y = x % 100;
y += (y < 38) ? 2000 : 1900;
return y;
}
每个范围都添加到用于填充日历的对象数组广告中,使用:
$.each(dateAdd, function (index, value) {
$('#calendar').fullCalendar('renderEvent', value, true);
});
最终代码:
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar !!!
drop: function (date, allDay) { // this function is called when something is dropped
// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
var firstDay = new Date(date.getTime());
while (firstDay.getDay() == 0 || firstDay.getDay() == 6) {
firstDay.setDate(firstDay.getDate() + 1);
}
var secondDay = new Date(firstDay.getTime());
do {
secondDay.setDate(secondDay.getDate() + 1);
} while (secondDay.getDay() == 0 || secondDay.getDay() == 6);
var thirdDay = new Date(secondDay.getTime());
do {
thirdDay.setDate(thirdDay.getDate() + 1);
} while (thirdDay.getDay() == 0 || thirdDay.getDay() == 6);
var dateAdd = new Array();
if (getWeekNr(firstDay)==getWeekNr(secondDay) && getWeekNr(firstDay)==getWeekNr(thirdDay)) {
var copiedEventObject = $.extend({}, originalEventObject);
copiedEventObject.start = firstDay;
copiedEventObject.end = thirdDay;
copiedEventObject.allDay = allDay;
dateAdd.push(copiedEventObject);
}
else {
if (getWeekNr(firstDay)==getWeekNr(secondDay)){
var copiedEventObject = $.extend({}, originalEventObject);
copiedEventObject.start = firstDay;
copiedEventObject.end = secondDay;
copiedEventObject.allDay = allDay;
dateAdd.push(copiedEventObject);
var copiedEventObject = $.extend({}, originalEventObject);
copiedEventObject.start = thirdDay;
copiedEventObject.end = thirdDay;
copiedEventObject.allDay = allDay;
dateAdd.push(copiedEventObject);
}
else {
var copiedEventObject = $.extend({}, originalEventObject);
copiedEventObject.start = firstDay;
copiedEventObject.end = firstDay;
copiedEventObject.allDay = allDay;
dateAdd.push(copiedEventObject);
var copiedEventObject = $.extend({}, originalEventObject);
copiedEventObject.start = secondDay;
copiedEventObject.end = thirdDay;
copiedEventObject.allDay = allDay;
dateAdd.push(copiedEventObject);
}
}
$.each(dateAdd, function (index, value) {
$('#calendar').fullCalendar('renderEvent', value, true);
});
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
}
});
工作演示: