我想在fullcalendar中添加一些事件。 aspx中的webmethod为js生成一个json 但我无法将网络方法的结果与完整的日历相关联, 我只是可以添加手册事件。
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'
}]
});})
首先,这个web方法中的参数是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"}]
答案 0 :(得分:0)
添加events: [<%=getPlanning%>]
并移除eventSources
。
答案 1 :(得分:0)
您应该尝试使用此代码仅获取给定范围内的事件,然后,回调应该具有魔力:
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参数替换服务器方法的签名...
答案 2 :(得分:0)
日历从未正确加载数据,因为它首先在没有事件的情况下加载完整日历。而且,我想我应该制作一个addEventSource ... 在document.ready(function)中移动了调用之后, 解决方案是在事件中获取json的结果而不是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);
}
});
})
如果您有任何建议让代码更好,我现在不要关闭这个主题。