簡體   English   中英

fullcalendar不顯示事件

[英]fullcalendar doesn't show events

我想向全日歷添加一些事件。 aspx中的一種web方法會生成一個json到js,但是我無法將web方法的結果與完整的日歷鏈接起來,我只能添加manuals事件。

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'
    }]
});})

首先,此網絡方法中的參數為String和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;
    }

我將json文件簽入jsonlint.com並進行了驗證,所以我猜錯誤出在js中,但我看不到哪里。

和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

您應該嘗試使用此代碼以僅獲取給定范圍內的事件,然后,回調應該具有魔力:

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);
           } 
        }
    }
}

為了使其工作,您必須用DateTime參數替換服務器方法的簽名...

日歷永遠不會正確加載數據,因為它首先被加載到沒有事件的完整日歷中。 而且,我想我應該做一個addEventSource ...在document.ready(function)中移動調用之后,解決方案是在事件而不是eventSource中獲取json的結果:

$(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);
    }
});
})

如果您有任何建議可以使代碼更好,那么我現在不關閉主題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM