14

单击 .agenda-views

我猜,什么时候点击下一个-上一个按钮,然后我会将当前发送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

4 回答 4

25

来自 FullCalendar 在线文档

FullCalendar 将在需要新事件数据时调用此函数。当用户单击上一个/下一个或切换视图时触发。

这个函数将被赋予开始结束参数,它们是 Moments ,表示日历需要事件的范围。

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

它还将被赋予callback,一个必须在自定义事件函数生成其事件时调用的函数。事件函数的职责是确保使用事件对象数组调用回调

下面是一个示例,展示了如何使用事件函数从假设的 XML 提要中获取事件:

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

资源


我做了一些小改动:

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

注意: start并且end 必须ISO 8601。另一个变化是使用format代替unix(这让我更容易处理代码隐藏)

于 2014-08-20T11:56:12.023 回答
3

有一个内置选项可用

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

更多细节https://fullcalendar.io/docs/events-json-feed

于 2019-12-04T12:15:20.963 回答
1

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

}

于 2020-08-03T23:17:26.680 回答
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    
});
于 2017-05-16T09:38:29.900 回答