我将JSON文件中的数据加载到我的Fullcalendar中没有问题,但我希望我的所有数据只显示在可拖动/可丢弃事件的外部列表上,然后用户决定将事件拖动到哪里(示例)
我确实实现了将数据加载到外部列表div。它显示正确,但当我将事件拖到日历中时,标题就会消失。如何避免数据丢失?
这是代码:
<script>
$(document).ready(function() {
/* initialize the external events
-----------------------------------------------------------------*/
$('#external-events div.external-event').each(function() {
// create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
// it doesn't need to have a start or end
var eventObject = {
title: $.trim($(this).text()) // use the element's text as the event title
};
// store the Event Object in the DOM element so we can get to it later
$(this).data('eventObject', eventObject);
// make the event draggable using jQuery UI
$(this).draggable({
zIndex: 999,
revert: true, // will cause the event to go back to its
revertDuration: 0 // original position after the drag
});
});
/* initialize the calendar
-----------------------------------------------------------------*/
$('#calendar').fullCalendar({
header: {
left: 'prevYear,nextYear prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
events: 'json/example.json',
eventRender: function (event, element) {element.find('.fc-event-title').html(event.id);},
eventClick: function(event) {
if (event.url) {
window.open(event.url);
return false;
}
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar !!!
drop: function(date, allDay) { // this function is called when something is dropped
// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
// we need to copy it, so that multiple events don't have a reference to the same object
var copiedEventObject = $.extend({}, originalEventObject);
// assign it the date that was reported
copiedEventObject.start = date;
copiedEventObject.allDay = allDay;
// render the event on the calendar
// the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
}
});
});
体内divs:
<div id='wrap'>
<div id='external-events' style="display: none;">
<h4>Draggable Events</h4>
<div class='external-event' id="mec1"></div>
<div class='external-event' id="mec2"></div>
<div class='external-event'>My Event 3</div>
<div class='external-event'>My Event 4</div>
</div>
<div id='calendar'></div>
<div style='clear:both'></div>
</div>
您可以看到,我还尝试使用外部JavaScript将JSON数据加载到div id"mec1"和div id"mec2"。这是成功的,但同样的情况。拖放后标题消失。
如果我把它们拖到div id='calendar',标题就不会再显示了。
我的JSON文件只包含以下内容:
[
{
"title" : "Blue Monday",
"allDay" : false
},
{
"title" : "John de John",
"allDay" : false
},
{
"title" : "Pumukli Pista",
"allDay" : false
}
]
我希望我提供了足够的信息。提前感谢您在这方面的帮助。
我对fullcalendar和jQuery,js非常熟悉。我试图从这里解决这个问题
确实可以,如果你为最近的浏览器这样做,你可以使用浏览器中的JSON库。这意味着你可以从JSON对象访问,你可以在解析服务器上的JSON对象后在这里看到。
所以你可以这样做:
var eventObjectFromDB = JSON.Parse(yourobject);
var eventToExternalEvents = {"title":eventObjectFromDB.title,
"id":eventObjectFromDB.id,
"start":eventObjectFromDB.start,
"end":eventObjectFromDB.end,
"allDay":eventObjectFromDB.allDay};
之后,使用第二个变量并将其添加到外部事件的数组/列表中。。。
这是你的要求吗?