如何使用Ajax在日历上加载所有事件?

时间:2012-08-18 13:55:03

标签: ajax fullcalendar

当我点击agenda-views中的 next-previous-button 时,我想使用AJAX加载FullCalendar上的所有事件。

我想,何时会点击 next-previous-button ,然后我会将当前date('y-m-d')发送到url: 'fetch-events.php',然后它会返回event{ id: ,title: , start: , end: , allDay: }格式数据用于在日历上呈现

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    selectable: false,
    selectHelper: false,
    editable: false,

    events: // on-click next-previous button load events using Ajax
    // post date using Ajax, then query to fetch all events and return data             
});

JSON在我的情况下不起作用

4 个答案:

答案 0 :(得分:12)

来自FullCalendar在线文档

  

FullCalendar会在需要新事件数据时调用此函数。   当用户单击prev / next或切换视图时会触发此操作。

     

此功能将被赋予开始结束参数   Moments表示日历需要事件的范围。

     

timezone 是描述日历当前的字符串/布尔值   时区。它是timezone选项的确切值。

     

它还将被赋予回调,这是一个必须在调用时调用的函数   自定义事件函数已生成其事件。这是事件   函数负责确保调用回调   一个Event Objects的数组。

     

这是一个示例,说明如何使用事件函数来获取   来自假设XML Feed的事件:

$('#calendar').fullCalendar({
    events: function(start, end, timezone, callback) {
        $.ajax({
            url: 'myxmlfeed.php',
            dataType: 'xml',
            data: {
                // our hypothetical feed requires UNIX timestamps
                start: start.unix(),
                end: end.unix()
            },
            success: function(doc) {
                var events = [];
                $(doc).find('event').each(function() {
                    events.push({
                        title: $(this).attr('title'),
                        start: $(this).attr('start') // will be parsed
                    });
                });
                callback(events);
            }
        });
    }
});

Source


我做了一些小改动:

$('#calendar').fullCalendar({
    events: function(start, end, timezone, callback) {
        jQuery.ajax({
            url: 'schedule.php/load',
            type: 'POST',
            dataType: 'json',
            data: {
                start: start.format(),
                end: end.format()
            },
            success: function(doc) {
                var events = [];
                if(!!doc.result){
                    $.map( doc.result, function( r ) {
                        events.push({
                            id: r.id,
                            title: r.title,
                            start: r.date_start,
                            end: r.date_end
                        });
                    });
                }
                callback(events);
            }
        });
    }
});

注意: startend 必须ISO 8601。另一个变化是使用format而不是unix(这使我更容易处理代码隐藏)

答案 1 :(得分:1)

This is perfect way to load data properly.

// if you want to empty events already in calendar.
$('#calendar').fullCalendar('destroy');

$.ajax({
    url: 'ABC.com/Calendar/GetAllCalendar/',
    type: 'POST',
    async: false,
    data: { Id: 1 },
    success: function (data) {
        obj = JSON.stringify(data);
    },
    error: function (xhr, err) {
        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        alert("responseText: " + xhr.responseText);
    }
});

/* initialize the external events
-----------------------------------------------------------------*/
$('#external-events div.external-event').each(function () {
    // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
    // it doesn't need to have a start or end
    var eventObject = {
        title: $.trim($(this).text()) // use the element's text as the event title
    };
    // store the Event Object in the DOM element so we can get to it later
    $(this).data('eventObject', eventObject);
    // make the event draggable using jQuery UI
    $(this).draggable({
        zIndex: 999,
        revert: true,      // will cause the event to go back to its
        revertDuration: 0  //  original position after the drag
    });
});

/* initialize the calendar
-----------------------------------------------------------------*/
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var calendar = $('#calendar').fullCalendar({
    //isRTL: true,
    buttonHtml: {
        prev: '<i class="ace-icon fa fa-chevron-left"></i>',
        next: '<i class="ace-icon fa fa-chevron-right"></i>'
    },
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    //obj that we get json result from ajax
    events: JSON.parse(obj)
    ,
    editable: true,
    selectable: true    
});

答案 2 :(得分:0)

有一个内置选项

var calendar = new FullCalendar.Calendar(calendarEl, {
    events: '/myfeed.php'
})

更多详细信息https://fullcalendar.io/docs/events-json-feed

答案 3 :(得分:0)

fullCalendar已经使用了ajax,因此您不必键入它。当我开始实现fullCalendar时,我在这里使用了投票率最高的答案的解决方案:

https://stackoverflow.com/a/25404081/3927450

但是我可以证明,fullCalendar负责在视图更改时进行ajax调用,而您无需执行任何操作。我觉得这个插件非常有用,尽管文档对我来说似乎不太清楚。

所以这段代码:

events: function(start, end, timezone, callback) {
    jQuery.ajax({
        url: 'schedule.php/load',
        type: 'POST',
        dataType: 'json',

正是这样:

events: schedule.php/load,

您只需提供网址。当然,您必须处理来自服务器的正确JSON响应。或者,如果您需要更多参数,可以这样做:

events: {
url: '/myfeed.php',
method: 'POST',
extraParams: {
  custom_param1: 'something',
  custom_param2: 'somethingelse'
},
failure: function() {
  alert('there was an error while fetching events!');
},
color: 'yellow',   // a non-ajax option
textColor: 'black' // a non-ajax option

}