javascript - FullCalendar 不通过 AJAX/JSON 呈现所有事件
问题描述
我一直在搞乱 FullCalendar 一段时间,但我有一个小问题。如果我通过 AJAX/JSON 以正确的格式加载事件,日历只会呈现第一个事件。
所有其他事件都显示在console.log
通话中,但日历仅显示第一个事件。文档中的示例事件代码如下:
events: [{
title: 'My Event',
start: '2010-01-01',
url: 'http://google.com/'
},
{
title: 'My Event',
start: '2010-01-02',
url: 'http://google.com/'
}
// other events here
],
我的代码如下:
events: function(start, end, timezone, callback) {
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$.ajax({
url: "/tande/fetch_holidays/",
type: 'POST',
success: function(data) {
var events = [];
$.map(data.holidays, function(value) {
console.log(value)
description = value.toString().split('_')[0]
start_date = value.toString().split('_')[1]
end_date = value.toString().split('_')[2]
person = value.toString().split('_')[3]
events.push({
title: person,
start: start_date,
end: end_date
});
callback(events);
console.log(events);
})
},
})
},
解决方案
事件的回调放置得太早了。正确代码如下:
events: function(start, end, timezone, callback) {
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$.ajax({
url: "/tande/fetch_holidays/",
type: 'POST',
success: function(data) {
var events = [];
$.map(data.holidays, function(value) {
console.log(value)
description = value.toString().split('_')[0]
start_date = value.toString().split('_')[1]
end_date = value.toString().split('_')[2]
person = value.toString().split('_')[3]
events.push({
title: person + ' - ' + description,
start: start_date,
end: end_date,
});
})
callback(events);
},
})
},
推荐阅读
- android - 为什么 arrayOf() 与 mutableListOf() 相同但有限?
- html - Vuetify 和 Layout 网格系统:垂直限制元素
- arrays - 整数和数组预期的一元运算符
- html - 如何链接同目录的 CSS 文件和 HTML 文件?
- c++ - 需要一个关于如何解决这个算法难题的想法
- c - 从关联指针数组中逐字符读取字符串
- c# - IdentityServer4 - 无法验证令牌
- python - 如何在 Pandas 中每 2 行执行加权平均?
- javascript - 将 GeoDjango 添加到现有的 Django 项目
- python - 如何在 django 中修复冻结的 cv2.imshow(来自 opencv)