笨蛋:https://plnkr.co/edit/xIfaWB?p=preview https://plnkr.co/edit/xIfaWB?p=preview
$(document).ready(function() {
// define the schedule;
// duration is days;
// title is what is shown on the calendar
// color is how to color event
var schedule = [{
duration: 4,
title: 'work',
color: 'red'
}, {
duration: 3,
title: 'off',
color: 'blue'
}, {
duration: 4,
title: 'work',
color: 'red'
}, {
duration: 3,
title: 'off',
color: 'blue'
}, {
duration: 4,
title: 'work',
color: 'red'
}, {
duration: 2,
title: 'off',
color: 'blue'
}, ];
// define the range of events to generate
var startDay = moment("2017-03-04");
var endDay = moment("2017-05-04");
// generate the events
var events = [];
// we loop from the start until we have passed the end day
// the way the code is defined, it will always complete a schedule segment
for (var s = 0, day = startDay; day.isBefore(endDay);) {
// loop for each day of a schedule segment
for (var i = 0; i < schedule[s].duration; i++) {
// add the event with the current properties
events.push({
title: schedule[s].title,
color: schedule[s].color,
// we have to clone because the add() call below mutates the date
start: day.clone(),
allday: true
});
// go to the next day
day = day.add(1, 'day');
}
// go to the next schedule segment
s = (s + 1) % schedule.length;
}
// render the calendar
$('#calendar').fullCalendar({
events: events
});
});