【问题标题】:How can I load all events on calendar using Ajax?如何使用 Ajax 加载日历上的所有事件?
【发布时间】:2012-08-14 16:32:51
【问题描述】:

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

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

$('#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 在我的情况下不起作用

【问题讨论】:

    标签: ajax fullcalendar


    【解决方案1】:

    来自 FullCalendar 在线文档

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

    这个函数会被赋予startend参数,它们是 Moments 表示日历需要事件的范围。

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

    它也会被赋予callback,一个必须在什么时候调用的函数 自定义事件函数已生成其事件。这是事件 函数负责确保 callback 被调用 Event Objects 的数组。

    这里是一个例子,展示了如何使用事件函数来获取 来自假设的 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);
                }
            });
        }
    });
    

    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(这让我更容易处理代码隐藏)

    【讨论】:

    • Source-link上有更多的例子和解释
    • 为什么在 !!doc.result 中使用双重否定?
    • 如何重新加载事件
    • @SbonisoMarcusNzimande 如果需要,您可以刷新所有内容。
    • @MichelAyres 问题不是刷新,我是根据过滤器显示日历。这意味着我从不同的链接获取事件,这导致复制事件时出现问题
    【解决方案2】:
    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    
    });
    

    【讨论】:

      【解决方案3】:

      有一个内置选项可用

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

      更多详情https://fullcalendar.io/docs/events-json-feed

      【讨论】:

        【解决方案4】:

        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
        

        }

        【讨论】:

        • 你不能用这个添加标题:)如果我错了,请纠正我:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 2020-10-19
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多