我已经研究了一个多星期,但我无法将我的JSON值拖动到外部可拖动的div中,也无法从Arshaw将其拖动到Fullcalendar中。
多亏了一些帮助,我把json数据加载到了外部div中。所有这些看起来都是正确的,但是在用"external events"类将动态创建的div附加到div之后,我的事件就不再可拖动了。
如果我将.draggable()
添加到我新创建的div中,并将其添加到具有'.external-event'
类的div,它将变得可拖动,但我无法将它们正确地放到日历中。一个标题值不可拖动的新div将出现在一个新的空白可拖动div下。这太烦人了。
在我的index.html:中
<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();
}
}
});
});
</script>
<style>
body {
margin-top: 40px;
text-align: center;
font-size: 14px;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
}
#wrap {
width: 1100px;
margin: 0 auto;
}
#external-events {
float: left;
width: 150px;
padding: 0 10px;
border: 1px solid #ccc;
background: #eee;
text-align: left;
}
#external-events h4 {
font-size: 16px;
margin-top: 0;
padding-top: 1em;
}
.external-event { /* try to mimick the look of a real event */
margin: 10px 0;
padding: 2px 4px;
background: #3366CC;
color: #fff;
font-size: .85em;
cursor: pointer;
}
#external-events p {
margin: 1.5em 0;
font-size: 11px;
color: #666;
}
#external-events p input {
margin: 0;
vertical-align: middle;
}
#calendar {
float: right;
width: 900px;
}
</style>
</head>
<body>
<div id='wrap'>
<div id='external-events' style="display: none;">
<img alt="web applications logo" src="tax4t.png">
<h4>Workes list</h4>
<div class='external-event'>John the first</div>
<div class='external-event'>Mark the second</div>
<div class='external-event'><button id="mecbtn" type="button">Mecanic Test</button> </div>
我使用myscript.js来获取JSON值并创建div。
window.onload = function () {
var html='';
$.getJSON('trax4t/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};
$('#external-events').append("<div class='external-event'>"+ eventToExternalEvents.title +"</div>");
$('.external-event').addClass('fc-event-draggable');
};
});
};
您可以看到这是一个典型的带有外部事件列表的Fullcalendar。
我现在很累了。我希望事情不要太乱。我的代码有更多//部分然后实际代码。我真的很努力。有人能告诉我我在这里缺了什么吗?
我希望这是足够的信息,但如果有必要,我会补充更多。提前感谢您!
这是我第一次回答自己的问题,但它对其他刚接触完整日历的人来说可能很有用,而且可能仍然需要更正,因为肯定有更好的解决方案。
在一个外部myscript.js中,我重复了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' );
}
});
这现在起作用了。Fullcalendar将为每个事件返回正确的标题。我对for循环中的每个事件使用单独的id,但可能会对从JSON文件加载的id进行更改。这由你决定。
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
});
});
}