简体   繁体   English

fullcalendar 版本 4:重新获取事件不会删除新添加的事件

[英]fullcalendar version 4: refetch events doesn't remove newly added event

I'm using fullcalendar version 4, and I've added a custom button to 'refresh' the calendar with current information/events.我正在使用 fullcalendar 版本 4,并且我添加了一个自定义按钮来使用当前信息/事件“刷新”日历。

Here's my button:这是我的按钮:

 customButtons: {
        myReloadButton: {
            text: 'refresh',
             click: function() {
                    calendar.refetchEvents();
                    calendar_mini.refetchEvents();
               }
            }

I have two calendars that I want to refresh as you can see.如您所见,我有两个日历要刷新。 It works fine, but I notice an issue.它工作正常,但我注意到一个问题。 When I create a new event, it's added to the calendar as it should.当我创建一个新事件时,它会按原样添加到日历中。 However, if the user clicks the 'refresh' button right afterwards then the just-added event shows up twice once the events are re-loaded.但是,如果用户随后立即单击“刷新”按钮,则在重新加载事件后,刚刚添加的事件会显示两次。 It seems the newly added event was never removed during the refresh.似乎在刷新期间从未删除新添加的事件。

I have a modal form that opens to gather information for the event.我有一个模式表单,可以打开以收集事件的信息。 Once the event is saved, here's how I am saving it (via an ajax call).保存事件后,这是我保存它的方式(通过 ajax 调用)。 In the success of the ajax, I am adding the event to my calendar with .addEvent()在 ajax 的成功中,我使用 .addEvent() 将事件添加到我的日历中

 var location_id = $('#formEventModal #location_id').val();
 var saw_by_id= $('#formEventModal #saw_by_id').val();

$.ajax({
            url: "ajax_insert.php?table=appointments",
            dataType: 'json',
            type: "POST",
            data: $('#appointmentForm').find(':input').not('.dont_serialize').serialize(),
            success: function(response) {
                if (response.success) {

                    //get the inserted id of the appt just added so it can be updated on the stop if needed.
                    var lastInsertID = response.lastInsertID;

                    var event_object = {
                        id: lastInsertID,
                        location_id: location_id,
                        resourceId: saw_by_id,
                    };

                        calendar.addEvent(event_object);
                        calendar_mini.addEvent(event_object);

Why is it that the refresh button doesn't remove newly added events?为什么刷新按钮没有删除新添加的事件?

The problem is that refetching events just reloads events from a defined source.问题是重新获取事件只是从定义的源重新加载事件。 It doesn't have any effect on standalone events which are not attached to a source.它对未附加到源的独立事件没有任何影响。

There are two ways to address this有两种方法可以解决这个问题

One solution is to replace calendar.addEvent(event_object);一种解决方案是替换calendar.addEvent(event_object); in the AJAX "success" function with simply calendar.refetchEvents();在 AJAX“成功”函数中,只需使用calendar.refetchEvents(); - then it immediately reloads from the server, which will include the newly added event, instead of adding a separate standalone event to the calendar directly. - 然后它立即从服务器重新加载,其中将包括新添加的事件,而不是直接向日历添加单独的独立事件。

Another option is to attach the event to the source when adding it to the calendar.另一种选择是在将事件添加到日历时将事件附加到源。 The Add Event documentation says添加事件文档

source represents the Event Source you want to associate this event with. source表示您希望将此事件与其关联的事件源。 It can be a string Event Source ID or an Event Source Object.它可以是字符串事件源 ID 或事件源对象。 When the source is refetched, it will clear the dynamically added event from the internal cache before fetching.当重新获取源时,它将在获取之前从内部缓存中清除动态添加的事件。 This parameter is optional.该参数是可选的。

So if you gave your event source an ID when you declared it, you could place that here as an extra option to associate the event with the source.因此,如果您在声明事件源时为其提供了 ID,则可以将其放置在此处作为将事件与源关联的额外选项。 Something like this:像这样的东西:

calendar.addEvent(event_object, 1);

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

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