I have seen the demo for drag elements https://fullcalendar.io/docs/external-dragging-demo
On this demo, My event 1..5 are static
Is it possible to dynamically populate the draggable external list of fullcalendar from the JSON file?
My idea is to show the TITLE as the name of my draggable event and to keep the others parameters are additional parameters from my Fullcalendar.
I've tried to dynamically create the divs of external events from code behind but it was not draggable anymore.
Thank you !
var JsonData = '[{"id":"1","title":"Event Title1","start":"2015-03-17T13:13:55.008","end":"2015-03-19T13:13:55.008"},{"id":"2","title":"Event Title 2","start":"2015-03-17T13:13:55-0400","end":"2015-03-19T13:13:55-0400"}]';
var obj = JSON.parse( JsonData );
var tmp = '';
$.each( obj, function( key, value ) {
tmp += '<div class="col-md-4 col-sm-4">';
tmp += ' <div class="fc-event" data-color="#00cc99">';
tmp += ' <h5>' + value.title + '</h5>';
tmp += ' </div>';
tmp += ' </div>';
});
$('#main').prepend(tmp);
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>
Drag-n-drop external events - Demos | FullCalendar
</title>
<link href='https://fullcalendar.io//assets/demo-topbar.css' rel='stylesheet' />
<link href='https://fullcalendar.io//releases/fullcalendar/3.9.0/fullcalendar.min.css' rel='stylesheet' />
<link href='/https://fullcalendar.io/releases/fullcalendar/3.9.0/fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='https://fullcalendar.io//releases/fullcalendar/3.9.0/lib/moment.min.js'></script>
<script src='https://fullcalendar.io//releases/fullcalendar/3.9.0/lib/jquery.min.js'></script>
<script src='https://fullcalendar.io//releases/fullcalendar/3.9.0/fullcalendar.min.js'></script>
<script src='https://fullcalendar.io//assets/demo-to-codepen.js'></script>
<script src='https://code.jquery.com/ui/1.11.3/jquery-ui.min.js'></script>
<script>
$(function() {
// initialize the external events
// -----------------------------------------------------------------
$('#external-events .fc-event').each(function() {
// store data so the calendar knows to render an event upon drop
$(this).data('event', {
title: $.trim($(this).text()), // use the element's text as the event title
stick: true // maintain when user navigates (see docs on the renderEvent method)
});
// 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: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar
drop: function() {
// 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>
html, body {
margin: 0;
padding: 0;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
font-size: 14px;
}
#external-events {
position: fixed;
z-index: 2;
top: 20px;
left: 20px;
width: 150px;
padding: 0 10px;
border: 1px solid #ccc;
background: #eee;
}
.demo-topbar + #external-events { /* will get stripped out */
top: 60px;
}
#external-events .fc-event {
margin: 1em 0;
cursor: move;
}
#calendar-container {
position: relative;
z-index: 1;
margin-left: 200px;
}
#calendar {
max-width: 900px;
margin: 20px auto;
}
</style>
</head><body>
<div class='demo-topbar'>
<button data-codepen class='codepen-button'>Edit in CodePen</button>
Drag external events into the calendar with the help of jquery-ui draggable
</div>
<div id='external-events'>
<p>
<strong>Draggable Events</strong>
</p>
<div class='main'>
</div>
<div class='fc-event'>My Event 1</div>
<div class='fc-event'>My Event 2</div>
<div class='fc-event'>My Event 3</div>
<div class='fc-event'>My Event 4</div>
<div class='fc-event'>My Event 5</div>
<p>
<input type='checkbox' id='drop-remove' />
<label for='drop-remove'>remove after drop</label>
</p>
</div>
<div id='calendar-container'>
<div id='calendar'></div>
</div>
</body>
</html>
.draggable
on each of those dynamically elements to make them draggable. Obviously you'll need to do that after you've appended them to the DOM – ADyson$("#main")
selector, yet this does not exist. I do not see an element with themain
ID. I do see one with amain
Class. – Twisty