如何在FullCalendar v3.8.0中获取Google Calendar事件的Location属性
解决FullCalendar v3.8.0获取Google Calendar Location属性的问题
我明白你遇到的问题——默认情况下FullCalendar的gcal插件不会把Google Calendar里的Location字段映射到事件对象中,修改EventDef.defineStandardProps只是让FullCalendar识别这个属性,但关键是要先让gcal插件把数据传进来。这里有两种可靠的解决方案:
方法1:使用eventDataTransform回调(推荐,无需修改插件源码)
FullCalendar的谷歌日历事件源支持eventDataTransform回调,你可以在这里手动把Google Calendar返回的location字段添加到事件对象里。修改你的events配置如下:
events: { googleCalendarId: '<<hidden>>', eventDataTransform: function(rawGcalEvent) { // 把Google Calendar的原始数据转换为FullCalendar事件对象 return { id: rawGcalEvent.id, title: rawGcalEvent.summary, start: rawGcalEvent.start.dateTime || rawGcalEvent.start.date, end: rawGcalEvent.end.dateTime || rawGcalEvent.end.date, url: rawGcalEvent.htmlLink, description: rawGcalEvent.description, location: rawGcalEvent.location // 关键:添加Location字段 }; } },
这样处理后,在eventRender里就能直接使用event.location了,比如更新你的qTip内容:
eventRender: function(event, element) { var qTipContent = event.description || ''; if (event.location) { qTipContent += '<br><strong>Location:</strong> ' + event.location; } element.qtip({ content: qTipContent, title: event.title, position: { my: 'center', at: 'center', target: $(window) }, show: 'click' }); },
方法2:修改gcal.js插件源码(适合愿意改动库文件的场景)
如果你不想用回调,也可以直接修改fullcalendar/gcal.js里的事件解析逻辑。找到文件中的parseGoogleCalendarEvent函数(类似名称的转换函数),在构建事件对象时添加location字段:
function parseGoogleCalendarEvent(item) { var event = { id: item.id, title: item.summary, start: item.start.dateTime || item.start.date, end: item.end.dateTime || item.end.date, url: item.htmlLink, description: item.description, location: item.location // 添加这一行 }; // 保留原函数的其他逻辑... return event; }
保存修改后刷新页面,就能在事件对象中拿到location属性了。
为什么之前修改EventDef.defineStandardProps没用?
EventDef.defineStandardProps是用来告诉FullCalendar哪些属性是事件的标准属性,但它不会自动从Google Calendar的数据源中拉取对应字段。你需要先确保gcal插件把location字段传递给FullCalendar的事件对象,这才是关键。
内容的提问来源于stack exchange,提问作者DoYouAdmit
相关产品推荐
相关产品推荐

