简体   繁体   English

fullcalendar js:使用ajax获取更多事件

[英]fullcalendar js: fetching more events with ajax

I'm using fullcalendar , how can I fetch more events from same server side, multiple urls? 我正在使用fullcalendar ,如何从同一服务器端,多个URL获取更多事件? The initial one works, I just want to add additional events when they arrive(ajax). 最初的方法有效,我只想在事件到达时添加其他事件(ajax)。

You could use Ajax to get the data and then add dynamically the new source 您可以使用Ajax获取数据,然后动态添加新源

$.ajax({
  url: "test.html",
  success: function(data){
       var source = { events: [
                            {
                                title: data.getTitle(),
                                start: new Date(year, month, day)
                            }
                ]};
                $('#calendar').fullCalendar( 'addEventSource', source );
  }
});

If each and every time you are using a different URL, then you can simply use addEventSource with the new URL. 如果您每次都使用不同的URL,则只需将addEventSource与新URL一起使用。

If you are attempting to use the same URL, you can get all events (old and new) using refetchEvents . 如果您尝试使用相同的URL,则可以使用refetchEvents获取所有事件(旧事件和新事件)。

You can also get the JSON and create the event as a client event using renderEvent . 您还可以获取JSON并使用renderEvent将事件创建为客户端事件。 The latter is the most "ajax-y" of the options. 后者是选项中最“ ajax-y”的选项。 In this case have your source return the JSON that represents the events, iterate through the array of new events, and call renderEvent on it. 在这种情况下,让您的源返回表示事件的JSON,遍历新事件的数组,然后在其上调用renderEvent。

// this call goes in a poll or happens when the something should trigger a
// fetching of new events
$.ajax({
  url: "path/to/event/source",
  success: function(data){
      $.each(data, function(index, event)
                $('#calendar').fullCalendar('renderEvent', event);
      );
  }
});

The ajax call done can also insert in the calendar code 完成的ajax调用也可以插入日历代码中

      $.ajax({ 
            url: url, 
            type: 'GET', 
            data: { }, 
            error: function() {
                alert('there was an error while fetching events!');
            } 
        }).done(function (doc) { 
                var event = Array();
                    $.each(doc, function(i, entry) 
                        event.push({title: entry.title, start: entry.start});
                    }); 
                 $('#calendar').fullCalendar({
                    header: {
                        left: 'prev,next',
                        center: 'title',
                        right: 'month,agendaWeek,agendaDay'
                    },
                    defaultDate: '2014-06-12',
                    editable: true,
                    events: event
                });

     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM