In this post we will be saving the event in a full calendar plugin using jQuery ajax and asp.net pagemethod or web service. To go through the post I request you to first check the following post. We are going the proceed forward with the same post and its source code.
http://growingtech.blogspot.in/2012/02/full-calendar-with-json-data-source.html
In addition to the previous script and CSS file references, we have added one more script file for this purpose-
Now how we are going to solve this issue is that on click of a day, we are going popup an entry form for the event and we will use ajax to save the event to the server and after successful saving we will be adding the event to the calender. Now calender plugin has some event to solve this purpose-
http://growingtech.blogspot.in/2012/02/full-calendar-with-json-data-source.html
In addition to the previous script and CSS file references, we have added one more script file for this purpose-
<script src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js" type="text/javascript"></script>This file is used hare to to JSON stringify the event object.
Now how we are going to solve this issue is that on click of a day, we are going popup an entry form for the event and we will use ajax to save the event to the server and after successful saving we will be adding the event to the calender. Now calender plugin has some event to solve this purpose-
- dayClick - this event happen during the mouse click of a day.
- renderEvent - using this event we can add an event to the calender. We can set stick to true to permanently add the event to the calender.
<div id="eventToAdd" style="display: none; font-size: 12px;"> Event name: <input id="eventName" type="text" /><br /> Event start date: <input id="eventStartDate" type="text" />(MM-dd-yyyy)<br /> Event end date: <input id="eventEndDate" type="text" />(MM-dd-yyyy)<br /> </div>Now we need to show popup dialog for adding event on the day click of the calendar day. To do so we will take the help of the dayClick event of the calender and it goes here-
<script type="text/javascript"> $('div[id*=fullcal]').fullCalendar({ dayClick: function (dateSelected, allDay, jsEvent, view) { $("#eventToAdd").dialog( { title: "Add event", modal: true, buttons: { "Add": function () { //event adding logic goes here } } }); } }); </script>Now on click of add button in the dialog we need to do two things. First save the event data using jQuery ajax and page method / web service. And secondly on the success of the ajax call add the event on the calender. Both the work is done here-
"Add": function () { var event = new Object(), eventToSave = new Object(); ; eventToSave.EventID = event.id = Math.floor(200 * Math.random()); event.start = new Date($("#eventToAdd #eventStartDate").val()); eventToSave.StartDate = $("#eventToAdd #eventStartDate").val(); if ($("#eventToAdd #eventEndDate").val() == "") { event.end = event.start; eventToSave.EndDate = eventToSave.StartDate; } else { event.end = new Date($("#eventToAdd #eventEndDate").val()); eventToSave.EndDate = $("#eventToAdd #eventEndDate").val(); } eventToSave.EventName = event.title = $("#eventToAdd #eventName").val(); $("#eventToAdd input").val(""); $.ajax({ type: "POST", contentType: "application/json", data: "{eventdata:" + JSON.stringify(eventToSave) + "}", url: "FullcalenderwithPagemethod.aspx/AddEvents", dataType: "json", success: function (data) { $('div[id*=fullcal]').fullCalendar('renderEvent', event, true); $("#eventToAdd").dialog("close"); }, error: function (XMLHttpRequest, textStatus, errorThrown) { debugger; } }); }What we are doing here is in line 2 we are creating two JavaScript objects event and eventToSave and line 3-14 we are assigning the values from the popup. And finally we are saving the data to server using ajax call. Why we have taken two objects here is event data of the calender has different property name and the server event have different name. Once the saving of event is successful we are adding the event object to the calendar using line number 24. The page method using for this is as follows-
[System.Web.Services.WebMethod] public static bool AddEvents(Event eventdata) { ListFor this example we have used in memory event object. We can easily replace it with proper database driven. The complete modified code for this is available here for download.events; if (HttpContext.Current.Session["events"] != null) events = (List )HttpContext.Current.Session["events"]; else events = new List (); events.Add(eventdata); HttpContext.Current.Session["events"] = events; return true; }
Hey Anup
ReplyDeleteGreat Post on fullcalendar
I was wondering how to retrieve the value of json object in php?
Hi,
DeleteI don't have any knowledge on PHP. But I think you can look into the following posts-
http://simpletutorials.com/?path=tutorials/javascript/simple_ajax
http://xoops.org/modules/news/article.php?storyid=5103
http://themekraft.com/getting-json-data-with-php-curl/
http://be2.php.net/file_get_contents
http://www.jquery4u.com/json/ajaxjquery-getjson-simple/#.T8SkIUUthrg
Quick question, do you know how to save the events in bulk via Json rather than one event at a time?.
ReplyDeleteYes you can do this. You have to decide how the entry from should be for bulk of event. And saving events is nothing but passing a List type data to the pagemethod. you can check this post-
Deletehttp://growingtech.blogspot.in/2012/01/post-complex-data-to-pagemethod-or.html
Hello Anup
ReplyDeleteI am new to Json & Jquery, so can you explain me how to display the time from morning 7 AM to 10 PM ?
Hi,
ReplyDeleteFirst of all enable day as default selection. Then get data to fill the event. While dragging an dropping you can compare your time range. It its exceeding the range do not allow to drop the event. Please refer the documentation.
Hi Anup,
ReplyDeleteThank you for such a valuable post but i need to know i want to create a time table,how could i do that?
Its a very general query. You can take help from this post and use ajax for creating timetable.
Delete