Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "post" fullcalendar data back to server?

I'm integrating FullCalendar into a web form.

FullCalendar gets it's data from a hidden form field. Like this:

var data = jQuery.parseJSON(jQuery('#fullcalendar_data').val());

Then FullCalendar does it's awesome, letting the user edit.

Once the user is complete, they click "Save" on the form.

How do I get FullCalendar's event data back into the hidden form field as JSON, so the "Save" posts it back to the server?

like image 213
Mike Avatar asked Dec 22 '22 15:12

Mike


2 Answers

Use this code to get the events from your calendar:

<script type='text/javascript'>
function savedata(){
$(document).ready(function () {
    $(function () {
        $("#save").click(function () {
            var eventsFromCalendar = $('#calendar').fullCalendar('clientEvents');
            alert(eventsFromCalendar);
            $.ajax(
        {

            url: '@Url.Action("Save")',
            type: 'POST',
            traditional: true,
            data: { eventsJson: JSON.stringify(eventsFromCalendar) },
            dataType: "json",
            success: function (response) {
                alert(response);
            },
            error: function (xhr) {
                debugger;
                alert(xhr);
            }

        });
        });
    });
});
 }
 </script> 

And make a controller method to receive the data like this:

[HttpPost]
public ActionResult Save(string eventsJson)
{
var events = JsonConvert.DeserializeObject<IEnumerable<Event>>(eventsJson);
return View();
}

 public class Event
{
    public int Id { get; set; }



    public string Title { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

And you can call this java script function on any event from your page like onclick by making it a function etc.

like image 191
jahanzaib kk Avatar answered Jan 05 '23 23:01

jahanzaib kk


@ppumkin that will NOT work since it is submitted as an object to the server side, you need to stringfy it

var eventsFromCalendar = $('#calendar').fullCalendar( 'clientEvents');

    var eventsForCookie = [];

    $.each(eventsFromCalendar, function(index,value) {
        var event = new Object();
        event.id = value.id;            
        event.start = value.start;
        event.end = value.end;
        event.title = value.title;
    event.allDay = value.allDay
        eventsForCookie.push(event);
    });             
    $("#all_events").val(JSON.stringify(eventsForCookie));

After that you can submit it to the server which you could parse using JSON.parse (Ruby)

like image 37
Maged Makled Avatar answered Jan 05 '23 21:01

Maged Makled