为了使用 fullcalendar 加载 .ics 文件,需要做什么? 不幸的是我无法使用 php 或 .net。
最佳答案
我设法做到了。没有我想象的那么难。我用ical.js作为 ics 解析器。解析后,我得到一个json对象,其中包含ics中的所有信息。然后遍历它并根据the definition of FullCalendar Event object构造事件对象.
使用 FullCalendar v4 和重复事件进行更新
作为 v4 changed the initialization code下面的代码不计入重复事件,这里有一个适用于 v4 版本的代码:
$.get(calendarUrl).then(function (data) {
// parse the ics data
var jcalData = ICAL.parse(data.trim());
var comp = new ICAL.Component(jcalData);
var eventComps = comp.getAllSubcomponents("vevent");
// map them to FullCalendar events
var events = $.map(eventComps, function (item) {
if (item.getFirstPropertyValue("class") == "PRIVATE") {
return null;
}
else {
var toreturn = {
"title": item.getFirstPropertyValue("summary"),
"location": item.getFirstPropertyValue("location"),
};
var rrule=item.getFirstPropertyValue("rrule");
if(rrule!= null){ //event recurs
toreturn.rrule={};
if(rrule.freq) toreturn.rrule.freq=rrule.freq;
if(rrule.parts.BYDAY) toreturn.rrule.byweekday=rrule.parts.BYDAY;
if(rrule.until) toreturn.rrule.until=rrule.until.toString();
if(rrule.until) toreturn.rrule.until=rrule.until.toString();
if(rrule.interval) toreturn.rrule.interval=rrule.interval;
var dtstart=item.getFirstPropertyValue("dtstart").toString();
var dtend=item.getFirstPropertyValue("dtend").toString();
toreturn.rrule.dtstart=dtstart;
//count duration ms
var startdate=new Date(dtstart);
var enddate=new Date(dtend);
toreturn.duration = enddate - startdate;
}else{
toreturn.start=item.getFirstPropertyValue("dtstart").toString();
toreturn.end=item.getFirstPropertyValue("dtend").toString();
}
return toreturn;
}
});
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'interaction','dayGrid','rrule' ],
defaultView: 'dayGridWeek',
displayEventEnd: true,
header: {
left: 'prev,next',
center: 'title',
right: 'dayGridDay,dayGridWeek,dayGridMonth'
},
events: events,
eventRender: function (info) {
// console.log(info.event);
// append location
if (info.event.extendedProps.location != null && info.event.extendedProps.location != "") {
info.el.append(info.event.extendedProps.location );
}
}
});
calendar.render();
});
原始答案的代码(v3 及更低版本):
$.get(calendarUrl).then(function (data) {
// parse the ics data
var jcalData = ICAL.parse(data.trim());
var comp = new ICAL.Component(jcalData);
var eventComps = comp.getAllSubcomponents("vevent");
// console.log(JSON.stringify(eventComps));
// map them to FullCalendar events
var events = $.map(eventComps, function (item) {
if (item.getFirstPropertyValue("class") == "PRIVATE") {
return null;
}
else {
return {
"title": item.getFirstPropertyValue("summary") + ";",
"start": item.getFirstPropertyValue("dtstart").toJSDate(),
"end": item.getFirstPropertyValue("dtend").toJSDate(),
"location": item.getFirstPropertyValue("location")
};
}
});
// refresh the control
calendarCtrl.fullCalendar('destroy');
calendarCtrl.fullCalendar({
events: events,
timeFormat: "H:mm",
displayEventEnd: true,
eventRender: function (event, element) {
// console.log(element);
// append location
if (event.location != null && event.location != "") {
element.append("<span>" + event.location + "</span>");
}
},
header: {
left: 'title',
center: '',
right: 'today,month,basicWeek,listDay prev,next'
}
});
});
关于javascript - 使用 fullcalendar 导入 iCal (ics)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9404685/