我正在为我的作家创建一个休假日历。 在此代码中,它是硬编码的并且当然可以工作
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 : [
{
title : 'All Souls Day',
start : new Date(y, m, 2),
backgroundColor: '#f56954', //red
borderColor : '#f56954' //red
},
],
感谢您的帮助。
最佳答案
好吧,我认为您使用 FullCalendar v4,如果我是对的,那么您需要回调来将事件数据传递到日历。像这样。
根据 DOCS,您需要有一个 successCallback
来将事件返回到日历。
Here is the docs https://fullcalendar.io/docs/events-function
由于我不知道您的 ajax 流程,因此我只是创建一个 API 来返回您的数据,然后将其传递给 successCallback
来在完整日历上正确打印事件,
注意:由于您的事件日期为 11/28 和 11,29,因此请导航至这些日期以查看事件。
Demo 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);
});
},
关于jquery - 将数组插入日历引导数组内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59024988/