【发布时间】:2013-12-06 11:40:52
【问题描述】:
我将数据从 JSON 文件加载到我的 Fullcalendar 没有问题,但是我希望我的所有数据只出现在可拖动/可放置事件的外部列表中,然后用户将决定将事件拖动到哪里 (example)
我确实实现了将数据加载到外部列表 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();
}
}
});
});
正文中的 div:
<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 有很强的基础。 我已经尝试解决问题from here
【问题讨论】:
-
请粘贴一些您收到的 JSON 以填充 div。
-
@HenriqueC。我现在确实粘贴了我的 JSON。这是基本但有效的。当我解决上述问题时,我将使用重要数据对其进行扩展。我不知道我不明白什么。
-
尝试添加“id”:“0”或“id”:“每个事件的id”。而 fullcalendar 也需要开始和结束。
-
感谢您的联系和帮助,但我不可能以某种方式将 json 数据传递给 var eventObject = { title: $.trim($(this).text()) }; ?
-
如果我必须重新表述我的问题,我会问“如何从加载的 JSON 文件构建外部事件列表?”。我希望这能让我更直接。
标签: javascript jquery json fullcalendar