简体   繁体   中英

fullcalendar doesn't show events

i want to add some event to the fullcalendar. A webmethod in aspx generate a json to the js But i can't link the result of the web method with the full calendar, I just can add manuals events.

the js :

$(document).ready(function () {
$('#btnInit').click(function () {
    var start = Date.parse($("#MainContent_dateD").text());
    var end = Date.parse($("#MainContent_dateF").text());
    var cle = $("#MainContent_HF_cleU").val();
    $.ajax({
        type: "POST",
        url: "ConsultationPlanning.aspx/getPlanning",
        data: '{"start": "' + start + '", "end": "' + end + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg == null) {
                alert('no result');
                return;
            }
            alert("received: " + msg.d);
            document.getElementById("MainContent_dateD").innerHTML = msg.d;
            $('#calendar').fullCalendar({
                eventSources: JSON.parse(msg.d)
            });
        },
        error: function(msg){
            alert("marche pas : " + msg);
        }
    });
});

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    hiddenDays: [0],
    defaultView: 'month',
    editable: false,
    allDaySlot: false,
    selectable: false,
    eventSources: [{
        url: 'ConsultationPlanning.aspx/getPlanning'
    }]
});})

firstly, parameters in this webmethods were String and the aspx.cs :

    public static String getPlanning(string start, string end)
    {
        List<String> ls1 = new List<string>();
        IList<Planning> ls2= new List<Planning>();

        DateTime t = Convert.ToDateTime(start);
        DateTime t2 = t.AddHours(1.0);
        Planning p=new Planning();

        for (int i = 0; i < 4; i++)
        {
            p.id = "" + i + "" ;
            p.title = "event "+i ;
            p.start = String.Format("{0:s}", t.AddDays(Convert.ToDouble(i)));
            p.end = String.Format("{0:s}", t2.AddDays(Convert.ToDouble(i)));
            ls2.Add(p);
        }
        System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        string sJSON = oSerializer.Serialize(ls2);
        return sJSON;
    }

I check the json file into jsonlint.com and it's validate, so i guess the mistake is in the js, but i don't see where.

and the json :

    [
    {"id":"0","title":"event 0","start":"2015-05-04T12:35:37","end":"2015-05-04T13:35:37"},
    {"id":"1","title":"event 1","start":"2015-05-05T12:35:37","end":"2015-05-05T13:35:37"},
    {"id":"2","title":"event 2","start":"2015-05-06T12:35:37","end":"2015-05-06T13:35:37"},
    {"id":"3","title":"event 3","start":"2015-05-07T12:35:37","end":"2015-05-07T13:35:37"}]

添加events: [<%=getPlanning%>]并删除eventSources

You should try this code to get only events in the given range, then, the callback should do the magic:

eventSources: {
    events: function (start, end, callback) {
        $.ajax({
           type: "POST",
           url: "ConsultationPlanning.aspx/getPlanning",
           data: '{ "start": "' + start.toISOString() + '", "end": "' + end.toISOString() + '" }',
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (msg) {
               callback(msg.d);
           } 
        }
    }
}

To make it work, you have to replace the signature of your server method with DateTime parameters...

The calendar never correctly load datas because, it was firstly load in the full calendar without events. and, i guess i should make a addEventSource... After have move the call in the document.ready(function), the solution was to get the result of the json in an event instead of the eventSource :

$(document).ready(function () {
var start = Date.parse($("#MainContent_dateD").text());
var end = Date.parse($("#MainContent_dateF").text());
var cle = $("#MainContent_HF_cleU").val();

$.ajax({
    type: "POST",
    url: "ConsultationPlanning.aspx/getPlanning",
    data: '{"start": "' + start + '", "end": "' + end + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function (msg) {
        if (msg == null) {
            alert('no result');
            return;
        }

        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            hiddenDays: [0],
            defaultView: 'month',
            editable: false,
            allDaySlot: false,
            selectable: false,
            events: JSON.parse(msg.d)
        });
    },

    error: function(msg){
        alert("function not working : " + msg);
    }
});
})

i don't close the subject now, if you have any suggestion to make the code better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM