Dynamically created external events not draggable

混江龙づ霸主 提交于 2019-12-06 12:27:18

This is the first time I answer my own question, but it can be useful to others who are new to fullcalendar and it may still need corrections as for sure there are better solutions to this.

In an external myscript.js I repeated the following part of the index.html:

window.onload = function () { 
$.getJSON('json/example.json', function(info){
   for (var numero = 0;numero < info.length;numero++) {
            var eventObjectFromDB = info[numero];
            var eventToExternalEvents =                      
                                           {"title":eventObjectFromDB.title,
                                "id":eventObjectFromDB.id,
                                "start":eventObjectFromDB.start,
                                        "end":eventObjectFromDB.end,
                                        "allDay":eventObjectFromDB.allDay,
                    "editable":true};

   $('#external-events').append("<div class='external-event' id='mec"+numero+"'>"+ eventToExternalEvents.title +"</div>");
            var eventObject2 = {
                title: $.trim(eventToExternalEvents.title) // use the element's text as the event title
            };
            $('#mec'+numero).data('eventObject', eventObject2);
            alert('#mec'+numero+'');
            $('.external-event').draggable({
                zIndex: 999,
                revert: true,      // will cause the event to go back to its
                revertDuration: 0  });
            $('#calendar').fullCalendar( 'refetchEvents' );
        }
    });

This now works. Fullcalendar will return the right title for each event. I use individual id's for each event from the for loop but probably I will change that for the id loaded from the JSON file. This is at your discretion.

Ait Friha Zaid
function externalevents() {
    $('#external-events .fc-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
        });
    });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!