javascript 从数组在全日历中设置事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23872898/
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
Set events in fullcalendar from array
提问by user3396420
I'm using fullcalendar, but I want to set events from array, for example:
我正在使用 fullcalendar,但我想从数组设置事件,例如:
var countries = new Array();
countries[0] = {'title':'Espa?a', 'start':new Date(y, m, d+4, 19, 0), url:'http://google.com/'};
countries[1] = {'title':'Portugal', 'start':new Date(y, m, 22, 22, 0)};
This is a static example, I'll get this array and in each case I'd have 3 or 9 or ... differents events.
这是一个静态示例,我会得到这个数组,在每种情况下我都会有 3 个或 9 个或......不同的事件。
But example is:
但例子是:
$('#calendar').fullCalendar({
editable: false,
events: [
{
title: 'example',
start: new Date(y, m, d+1, 19, 0),
end: new Date(y, m, d+1, 22, 30),
allDay: false
},
{
title: 'Click for Google',
start: new Date(y, m, 28),
end: new Date(y, m, 29),
url: 'http://google.com/'
},
]
});
How can I remove this two example events and set only my array "countries"?
如何删除这两个示例事件并仅设置我的数组“国家/地区”?
Is it possible?
是否可以?
回答by Kahosk
Removes events from the calendar.
从日历中删除事件。
$("#calendar").fullCalendar( 'removeEvents' [, idOrFilter ] )
Dynamically adds an event source.
动态添加事件源。
$("#calendar").fullCalendar( 'addEventSource', source )
From here: https://fullcalendar.io/docs/addEventSource
回答by MKrup
If you are using fullcalendar v4 there are a bunch of API changes mentioned in a Github issue.
如果您使用 fullcalendar v4,则 Github问题中提到了大量 API 更改。
You would need to use the new API to get all of the event objects from the calendar and call the remove method on all of them.
您需要使用新的 API 从日历中获取所有事件对象,并对所有这些对象调用 remove 方法。
After removing you need to use calendar.addEvent(event)
to add all of your events.
删除后,您需要使用calendar.addEvent(event)
来添加所有事件。
We don't want to re-render the full calendar after adding each event so we wrap this into a batch rendering which only causes one re-rendering.
我们不想在添加每个事件后重新渲染完整日历,因此我们将其包装到只导致一次重新渲染的批处理渲染中。
newEvents
is the array of the events you want to add.
newEvents
是要添加的事件数组。
// batch every modification into one re-render
calendar.batchRendering(() => {
// remove all events
calendar.getEvents().forEach(event => event.remove());
// add your new events
newEvents.forEach(event => calendar.addEvent(event));
});
Hope this helped someone :)
希望这对某人有所帮助:)