I'm using FullCalendar's external drag and drop, with his code. http://arshaw.com/js/fullcalendar-1.5.2/demos/external-dragging.html
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.c开发者_C百科om/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();
}
}
Every thing is okay, but I want to add this dropped event to my database. So I added my add dialog codes here.
var eventToAdd = {
title: $.trim($(this).text()),
//title: $(this).attr('id'),
description: $(this).attr('original-title'),
start: date.format("dd-MM-yyyy hh:mm:ss tt"),
end: date.format("dd-MM-yyyy hh:mm:ss tt")
};
if (checkForSpecialChars(eventToAdd.title) || checkForSpecialChars(eventToAdd.description)) {
alert("please enter characters: A to Z, a to z, 0 to 9, spaces");
}
else {
//alert("sending " + eventToAdd.title);
PageMethods.addEvent(eventToAdd, addSuccess);
}
So the result is,
drop: function (date, allDay) { // this function is called when something is dropped
if($(this).attr('id')=='')
return;
// 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;
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
$(this).remove();
var eventToAdd = {
title: $.trim($(this).text()),
//title: $(this).attr('id'),
description: $(this).attr('original-title'),
start: date.format("dd-MM-yyyy hh:mm:ss tt"),
end: date.format("dd-MM-yyyy hh:mm:ss tt")
};
if (checkForSpecialChars(eventToAdd.title) || checkForSpecialChars(eventToAdd.description)) {
alert("please enter characters: A to Z, a to z, 0 to 9, spaces");
}
else {
//alert("sending " + eventToAdd.title);
PageMethods.addEvent(eventToAdd, addSuccess);
}
}
Event is shown, event is draggable, but it doesn't get the Id. I think the event which rendered at this row isn't related with the event sent to the database:
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
FYI you can also set an id for the draggable div in your HTML, then do something like:
copiedEventObject.id = $( this ).attr('id');
inside the "drop:" attribute (right after:
var copiedEventObject = $.extend({}, originalEventObject);
in the "external-dragging.html" example.
I had a similar problem, not when dropping an external element, but when creating a new event by clicking on a time slot.
If you require the id, you must not render directly the event dropped, but obtain its data (title, start, end, allday), save it to the database, retrieve the saved event and render it. This way it will have the ID associated to it.
The method used to save it to the database, must return the event information required by fullcalendar to be rendered.
This is the code I have used:
select: function(start, end, allDay) {
//code required to initialise the variables used in the ajax call
//...
//URL used to call my cakephp controller/action along with the data required to save the event to the database
var url = "/eugdef/availabilities/add?start="+startyear+"-"+startmonth+"-"+startday+" "+starthour+":"+startminute+":00&end="+endyear+"-"+endmonth+"-"+endday+" "+endhour+":"+endminute+":00&allday="+allday+"&teacher_id="+teacher_id+"&teacher_name="+teacher_name+"&semester="+semester+"&year_id="+year_id;
$.post(
url,
function(data){ //callback function retrieving the response sent by the controller/action
//event is the object that will actually be rendered to the calendar
var event = jQuery.parseJSON(data.substr(1,data.length-2));
//actually rendering the new event to the calendar
calendar.fullCalendar('renderEvent', event, false);
calendar.fullCalendar('unselect');
}
);
}
I hope this helps
For external events, the drop
callback is only for the "low-level" dropping data. The eventReceive
callback is what you want:
eventReceive: function(event, view) {
alert("Dropped event: " + event['title']); // any data linked to the dropped event
},
Docs: https://fullcalendar.io/docs/dropping/eventReceive/
精彩评论