简体   繁体   English

fullcalendar 动态添加事件

[英]fullcalendar add events dynamically

I'm trying to create events in my full calendar dynamically.我正在尝试在我的完整日历中动态创建事件。

I have:我有:

$('#calendar').fullCalendar({
                viewRender: function (view) {

                    var h;
                    if (view.name == "month") {
                        h = NaN;
                    }
                    else {
                        h = 2500; // high enough to avoid scrollbars
                    }


                    $('#calendar').fullCalendar('option', 'contentHeight', h);
                },
                lang: 'fr',
                events: [
                    {
                        title: '8 présents',
                        start: data[0]
                    },
                    {
                        title: '8 excusés',
                        start: data[1]
                    },
                    {
                        title: '8 excusés',
                        start: '2015-01-08'
                    },
                    {
                        title: '8 présents',
                        start: '2015-01-08'
                    },
                ],
                dayClick: function (date, jsEvent, view) {
                    window.location.replace(Routing.generate('dateChoisie', {date: date.format()}));
                }
            })

I have a var data, which is an array that contains all the dates of the events.我有一个 var 数据,它是一个包含所有事件日期的数组。 I want to insert this in the events in the same way I inserted data[0], data[1], etc, but dynamically for all the dates.我想以与插入 data[0]、data[1] 等相同的方式将其插入到事件中,但对于所有日期都是动态的。

I have tried to do a for :我试图做一个for

events: [
    for (var i = 0, max = data.Lenght; i < max; i++) {
         {
                 title: '8 présents',
                 start: data[i]
           },
    }
                                {
                                    title: '8 excusés',
                                    start: data[1]
                                },
                                {
                                    title: '8 excusés',
                                    start: '2015-01-08'
                                },
                                {
                                    title: '8 présents',
                                    start: '2015-01-08'
                                },
                            ],

But it doesn't work inside the list.但它在列表中不起作用。

Anybody know how I can do this?有人知道我该怎么做吗?

after rendering the full calendar you can add events dynamically.渲染完整日历后,您可以动态添加事件。

var event={id:1 , title: 'New event', start:  new Date()};

$('#calendar').fullCalendar( 'renderEvent', event, true);

I was searching for a while and I have found an possibility.我搜索了一段时间,我找到了一种可能性。

It was very easy at the end...最后还是很轻松的。。。

I let this here, maybe anybody is interested in.我把这个放在这里,也许有人感兴趣。

for (var i in data)

    var monthSource = new Object();
    monthSource.title = data[i]+' présents'; 
    monthSource.start = i; // this should be date object
    monthSource.end = new Date(y, m, d); //to now
    month[a] = monthSource;
    a++;

    }
$('#calendar').fullCalendar({
viewRender: function (view) {

    $('#calendar').fullCalendar( 'removeEvents');
    $('#calendar').fullCalendar('addEventSource', month);
}

Source: http://fullcalendar.io/docs/event_data/addEventSource/来源: http : //fullcalendar.io/docs/event_data/addEventSource/

You can dynamically add an event source.您可以动态添加事件源。 An Event Source is an url which can for example return json data.事件源是一个 url,例如可以返回 json 数据。

Maybe it might be sufficient for you to fire the refetch event after you changed the event data.也许在更改事件数据后触发refetch事件可能就足够了。

.fullCalendar( 'refetchEvents' )

Source: http://fullcalendar.io/docs/event_data/refetchEvents/来源: http : //fullcalendar.io/docs/event_data/refetchEvents/

(The accepted solution will lose the event if you do anything complicated; the event added is ephemeral and will spontaneously disappear if you blink too hard. This solution is robust and will work if you do more complicated things.) (如果你做任何复杂的事情,被接受的解决方案将丢失事件;添加的事件是短暂的,如果你眨得太厉害就会自发消失。这个解决方案是强大的,如果你做更复杂的事情,它会起作用。)

Support for persistent events is a bit inelegant.对持久事件的支持有点不优雅。 You may have to dump, reload, AND render the entire calendar state...:您可能需要转储、重新加载和呈现整个日历状态...:

var CAL, EVENTS;

$(document).ready(function() {
    // set up calendar with an EventSource (in this case an array)
    EVENTS = [...];
    $('#calendar').fullCalendar({...});

    // calendar object
    CAL = $('#calendar').fullCalendar('getCalendar');
    // extend object (could be its own function, etc.)
    CAL.refresh = function() {
        CAL.removeEvents();
        CAL.addEventSource(EVENTS);
    }

    // finish setting up calendar
    CAL.refresh();
});

Demo:演示:

EVENTS.pop();  // remove last event
refresh();     // refresh calendar; last event no longer there

see https://stackoverflow.com/a/18498338https://stackoverflow.com/a/18498338

How about doing as it says on the website example:如何按照网站示例中的说明进行操作:

https://fullcalendar.io/docs/renderEvent-demo https://fullcalendar.io/docs/renderEvent-demo

So add the event, and then use whatever you want to add that to the backend.因此,添加事件,然后使用您想要将其添加到后端的任何内容。

Or you can add the event to backend, then return the database's new id and then add it to the timeline, so you'll have the ids right.或者您可以将事件添加到后端,然后返回数据库的新 id,然后将其添加到时间线,这样您就可以拥有正确的 id。

Or update the id with return message, whatever rocks your boat.或者用返回消息更新 id,无论你的船如何。

although it is not specified on the fullcalender site, it is necessary to assign a value to the "allday" parameter to be able to add new events dynamically.尽管在 fullcalender 站点上未指定它,但必须为“allday”参数分配一个值,以便能够动态添加新事件。 If you set this value to "false", it will not add the event to the AllDay row.如果将此值设置为“false”,则不会将事件添加到 AllDay 行。 If you do "true" it will add to the AllDay row.如果您执行“true”,它将添加到 AllDay 行。

var event = {
        title: 'New Event',
        start: Date(Date.now()),
        backgroundColor: App.getLayoutColorCode('purple'),
        allDay: false
    }
    jQuery('#calendar').fullCalendar('renderEvent',event,true);

or或者

var originalEventObject = jQuery(this).data('eventObject');
var copiedEventObject = jQuery.extend({}, originalEventObject);

copiedEventObject.title = "New Event";
copiedEventObject.start = date;
copiedEventObject.className = jQuery(this).attr("data-class");
copiedEventObject.backgroundColor = App.getLayoutColorCode('purple');
copiedEventObject.allDay = false;
jQuery('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

Simple examples of adding events can be found in the example-projects repo .可以在example-projects repo 中找到添加事件的简单示例。 There's currently examples for angular, vue, react, and bootstrap.目前有 angular、vue、react 和 bootstrap 的示例。

Wanted to mention this for anyone not using jquery who stumbles upon this想向任何不使用 jquery 的人提到这一点,他们偶然发现了这一点

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

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