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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8218224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:40:52  来源:igfitidea点击:

fullcalendar js: fetching more events with ajax

javascriptjsonfullcalendar

提问by kecske

I'm using fullcalendar, how can I fetch more events from same server side, multiple urls? The initial one works, I just want to add additional events when they arrive(ajax).

我正在使用fullcalendar,如何从同一服务器端、多个 url 获取更多事件?最初的一个有效,我只想在它们到达时添加额外的事件(ajax)。

回答by zaoudis

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 );
  }
});

回答by justkt

If each and every time you are using a different URL, then you can simply use addEventSourcewith 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. The latter is the most "ajax-y" of the options. 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将事件创建为客户端事件。后者是最“ajax-y”的选项。在这种情况下,让您的源返回表示事件的 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);
      );
  }
});

回答by sandhya murugesan

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
                });

     });