如何根据日历模式创建活动?

时间:2017-03-13 21:00:08

标签: javascript logic fullcalendar momentjs

我正在尝试为某人创建一个“转移”日历,我知道模式开始的那一天,我知道开启和关闭日期的模式。但我遇到麻烦将其转化为代码。

他们工作4天,下班3天,工作4天,下班3天,工作4天,休息2天,重复。我需要创建一些逻辑来基于此创建日历事件。

这就是我所拥有的:

$(document).ready(function() {

  var on = [4, 4, 4];
  var off = [3, 3, 2];
  var startPattern = "2017-03-04";

  var days = $('#calendar').fullCalendar('getDate').daysInMonth();
  var events = [];

  for (var i = $('#calendar').fullCalendar('getDate').day(); i < days; i++) {
    var event = {
      title: "work",
      start: ''
    }
    events.push(event);
  }


  $('#calendar').fullCalendar({
    // put your options and callbacks here
    events: events

  });
});

1 个答案:

答案 0 :(得分:0)

Plunker: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
  });

});