如何fullcalendar fc-event cut

时间:2016-02-25 05:13:00

标签: fullcalendar

fullcalendarAPI正在使用中。如果存在开发问题,这将写一个句子。 虽然暴露日历时fc事件中的事件会暴露给日历,但我们希望防止那个时间,土壤,星期日的表达。有办法....?

示例:

enter image description here

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将事件开始/结束并将其分解为多个部分。

https://jsfiddle.net/31gayu5b/4/

/* Fiddle specs: fullcalendar v2.4.0, moment v2.10.6, jQuery v2.1.4 */

/* If chop is true, it cuts out Saturday, Sunday */
var events = [{
  start: '2016-02-01',
  end: '2016-03-01',
  title: 'This is the whole month long, no weekends!',
  id: 'month-long-no-weekends',
  chop: true
}, {
  start: '2016-02-01',
  end: '2016-03-01',
  title: 'This is the whole month long, including weekends!',
  id: 'month-long-weekends',
  chop: false
}, {
  start: '2016-02-07',
  end: '2016-02-16',
  title: 'Starts Sunday ends Tuesday the week after (no weekends)',
  id: 'start-sun-no-weekends',
  chop: true
}, {
  start: '2016-02-07',
  end: '2016-02-16',
  title: 'Starts Sunday ends Tuesday the week after (has weekends)',
  id: 'start-sun-weekends',
  chop: false
}];

$('#calendar').fullCalendar({
  defaultDate: '2016-02-01',
  events: chopEvents(events),
  eventClick: function(event, jsEvent, view) {
    alert(event.id);
  },
  eventRender: function(event, element, view) {
    console.log(event);
    if (event.chop)
      element.css('background-color', 'green');
  }
});

function chopEvents(events) {
  var chopped = [];
  $(events).each(function() {
    var event = this;
    if (this.chop) {
      var segments = makeChunks(this);
      $(segments).each(function(index, dates) {
        var start = dates.shift();
        var end = dates.pop();
        event.start = start;
        event.end = end;
        chopped.push($.extend({}, event));
      });
    } else {
      chopped.push(event);
    }
  });
  return chopped;
}

function makeChunks(event) {
  var seg = 0;
  var segments = [];
  var start = moment(event.start);
  var end = moment(event.end); //.add(1, 'day'); /* May want to add 1 day? */
  for (var day = moment(start); day <= end; day = day.add(1, 'day')) {
    var dayOfWeek = day.format('E');
    if (dayOfWeek == 7) {
      segments[++seg] = [];
      continue;
    }
    if (segments[seg] === undefined) {
      segments[seg] = [];
    }
    segments[seg].push(day.format('YYYY-MM-DD'));
  }
  segments = segments.filter(function(i) {
      return i !== null && i.length > 1;
    })
    // console.log(JSON.stringify(segments, null, 2));
  return segments;
}