我正在为我的作家休假创建日历。 在这段代码中,它是经过硬编码的,并且可以正常工作
var calendar = new Calendar(calendarEl, {
plugins: [ 'bootstrap', 'interaction', 'dayGrid', 'timeGrid' ],
header : {
left : 'prev,next today',
center: 'title',
right : 'dayGridMonth,timeGridWeek,timeGridDay'
},
//Random default events
events : [
{
title : 'All Souls Day',
start : new Date(y, m, 2),
backgroundColor: '#f56954', //red
borderColor : '#f56954' //red
},
],
editable : true,
droppable : true, // this allows things to be dropped onto the calendar !!!
drop : function(info) {
// is the "remove after drop" checkbox checked?
if (checkbox.checked) {
// if so, remove the element from the "Draggable Events" list
info.draggedEl.parentNode.removeChild(info.draggedEl);
}
}
});
现在,我编写了此脚本(AJAX)以从数据库中提取记录
var arrevents = [];
$.get( "http://localhost/api/public/events", function( data ) {
var response = JSON.parse(data);
$.each(response, function(k, v) {
arrevents.push(v);
});
});
console.log(arrevents);
我的问题是如何使用第一个代码中的events数组将这些结果放入日历中。
我想将结果放在此变量中
events : [
{
title : 'All Souls Day',
start : new Date(y, m, 2),
backgroundColor: '#f56954', //red
borderColor : '#f56954' //red
},
],
感谢您的帮助。
答案 0 :(得分:5)
好的,我认为您使用FullCalendar v4,如果我说对了,那么您需要回调以将事件数据传递到日历。这样。
根据DOCS,您需要有一个successCallback
,它将事件返回到日历。
由于我不知道您的ajax进程,我只是制作了一个API,该API返回您的数据,然后将其传递到successCallback
,以便在完整日历上正确打印事件,
注意:由于您的活动日期在11/28和11,29上,因此请导航至这些日期以查看活动。
演示https://codepen.io/nasser-ali-karimi/pen/qBBGVbG?editors=0010
events: function(info, successCallback, failureCallback) {
var arrevents = [];
jQuery.get( "https://api.myjson.com/bins/16ubhe", function( data ) {
// var response = JSON.parse(data);
// $.each(response, function(k, v) {
// arrevents.push(v);
// });
arrevents = data;
successCallback(arrevents);
});
},