如何从加载的JSON文件构建外部事件列表?

时间:2013-12-06 11:40:52

标签: javascript jquery json fullcalendar

我将数据从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 in body:

<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 
} 
]

我希望我提供了足够的信息。提前感谢您对此的任何帮助。

我对使用jQuery,js的fullcalendar和强大的基础非常新。 我试图解决问题from here

1 个答案:

答案 0 :(得分:1)

确实,你可以,如果你在最近的浏览器中这样做,你可以使用浏览器中的JSON库。在解析从服务器传入的JSON对象后,您可以从JSON对象访问,这样您就可以看到here

所以你可以这样做:

var eventObjectFromDB = JSON.Parse(yourobject);
var eventToExternalEvents = {"title":eventObjectFromDB.title,
                             "id":eventObjectFromDB.id,
                             "start":eventObjectFromDB.start,
                             "end":eventObjectFromDB.end,
                             "allDay":eventObjectFromDB.allDay};    

在此之后使用第二个变量并将其添加到您的数组/外部事件列表中......

这是你的要求吗?