添加/删除事件源 FullCalendar

问题描述 投票:0回答:3

我希望能够根据添加和删除事件源来过滤事件。我找不到这样做的好例子。

.fullCalendar( 'addEventSource', source )
.fullCalendar( 'removeEventSource', source )

我想要有复选框来切换这些功能的执行。但我似乎无法让该功能正常工作。

$( "#target" ).click(function() { 
  $('#calendar').fullCalendar( 'removeEventSource', 'Event1' );
});

这是我的完整代码:

    $('#calendar').fullCalendar({
      header: {
    left: 'title',
    center: 'prev,next',
    right: 'month,agendaWeek,agendaDay,today'
},
       eventLimit: {
    'agenda': 4, // adjust to 6 only for agendaWeek/agendaDay
    'default': true // give the default value to other views
        },
          eventSources: [
            {
               title: 'Event1',
                url: "http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic"
            },

            {

                url: 'https://www.google.com/calendar/feeds/vineyardcincinnati.com_o6jncckm5ka55fpragnbp4mk9c%40group.calendar.google.com/public/basic'
            },
        {
        url: "https://www.google.com/calendar/feeds/ht3jlfaac5lfd6263ulfh4tql8%40group.calendar.google.com/public/basic"
            }
        ],

        eventClick: function(event) {
            // opens events in a popup window
            window.open(event.url, 'gcalevent', 'width=700,height=600');
            return false;
        },

        loading: function(bool) {
            if (bool) {
                $('#loading').show();
            }else{
                $('#loading').hide();
            }
        }

    });
fullcalendar
3个回答
2
投票

这是我用来获得此功能的完整代码:

HTML:

<form id="#calendar_list">
<input class="checkbox" type="checkbox"  checked>Event Group 1<br> 
<input class="checkbox1" type="checkbox"  checked>Event Group 2<br> 
<input class="checkbox2" type="checkbox"  checked>Event Group 3<br> 
</form>

Javascript:

    $(".checkbox").change(function() {
    if(this.checked) {
      $('#calendar').fullCalendar( 'addEventSource',  'https://www.google.com/calendar/feeds/vineyardcincinnati.com_o6jncckm5ka55fpragnbp4mk9c%40group.calendar.google.com/public/basic' );
    }
    else{
    $('#calendar').fullCalendar( 'removeEventSource',  'https://www.google.com/calendar/feeds/vineyardcincinnati.com_o6jncckm5ka55fpragnbp4mk9c%40group.calendar.google.com/public/basic' );
    }
});

2
投票

加载 FullCalendar : 使用以下给定代码加载 FullCalendar。创建一个像 LoadCalendar 这样的 jquery 函数,并将以下代码放入该函数中,并在 jquery 中的 document.ready 函数上调用该函数。

 $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        locale: '@companyCulture',
        defaultDate: Date.now(),
        defaultView: 'month',
        navLinks: true, // can click day/week names to navigate views
        editable: false,
        eventLimit: true, // allow "more" link when too many events
        dayClick: function (date, allDay, jsEvent, view) {
            //$("#lblDate").html('' + moment(date).format("MMMM DD,YYYY hh:mm") + '');
            $("#lblDate").html('' + moment(date).format("MMMM DD,YYYY hh:mm A") + '');
            $("#hdRDate").val(moment(date).format());
            emptyEventDetails(date);
            // $("#AddEventModel").modal();
        },
        eventClick: function (calEvent, jsEvent, view) {
            $.ajax({
                type: "GET",
                async: false,
                cache: false,
                url: "@Url.Action("GetEventById", "Events")",
                data: {
                    Eventid: calEvent.id
                },
                success: function (data) {
                    emptyEventDetails();
                    //$.each(data.data, function () {
                    //    alert(this["Title"]);
                    //    var color = 'orange';
                    //    var Title = this["Title"];
                    //    //addCalanderEvent(this["EventID"], this["EventDate"], Title, color);
                    //});

                }

            });

            //$("#lblDate").html('' + calEvent.EventDate + '');
            //$("#hdRDate").val(calEvent.EventDate);
            //$("#AddEventModel").modal();
        }
    });

添加事件:使用以下代码在 FullCalendar 中添加事件

 var eventObject = {
        title: title,
        start: moment(start).format("MMMM DD,YYYY hh:mm A"),
        end: moment(end).format("MMMM DD,YYYY hh:mm A"),
        id: id,
        color: colour
    };

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

$('#calendar').fullCalendar( 'addEventSource', newSource); //Add a new source 

删除所有事件:我正在尝试删除fullcalendar插件中的所有事件源。我目前正在使用

的组合
 $('#calendar').fullCalendar('removeEvents') //Hide all events
 $('#calendar').fullCalendar('removeEventSource', $('.Source').val()) //remove eventSource from stored hidden input

$('#Calendar').fullCalendar( 'removeEvents').fullCalendar('removeEventSources');  //Removes all event sources

0
投票

提供的代码使用 jQuery 语法初始化 FullCalendar 实例,但我已将其转换为 React Js

import React, { useEffect, useRef } from 'react';
import FullCalendar from '@fullcalendar/react';
import dayGridPlugin from '@fullcalendar/daygrid';
import googleCalendarPlugin from '@fullcalendar/google-calendar';

const YourCalendarComponent = () => {
  const calendarRef = useRef(null);

  useEffect(() => {
    const calendarApi = calendarRef.current.getApi();

    const eventSources = [
      {
        googleCalendarId: "[email protected]",
        className: 'event1'
      },
      {
        googleCalendarId: "vineyardcincinnati.com_o6jncckm5ka55fpragnbp4mk9c@group.calendar.google.com",
        className: 'event2'
      },
      {
        googleCalendarId: "[email protected]",
        className: 'event3'
      }
    ];

    const calendarOptions = {
      plugins: [dayGridPlugin, googleCalendarPlugin],
      headerToolbar: {
        left: 'title',
        center: 'prev,next',
        right: 'month,agendaWeek,agendaDay,today'
      },
      eventLimit: {
        agenda: 4,
        default: true
      },
      eventSources: eventSources,
      eventClick: function (event) {
        window.open(event.event.url, 'gcalevent', 'width=700,height=600');
        return false;
      },
      loading: function (bool) {
        if (bool) {
          // Handle loading indicator if needed
        } else {
          // Hide loading indicator
        }
      }
    };

    if (calendarApi) {
      calendarApi.setOption('googleCalendarApiKey', 'YOUR_GOOGLE_CALENDAR_API_KEY');
      calendarApi.setOption('eventSources', eventSources);
    }
  }, []);

  return (
    <div>
      <FullCalendar
        ref={calendarRef}
        plugins={[dayGridPlugin, googleCalendarPlugin]}
        initialView="dayGridMonth"
        headerToolbar={{
          left: 'title',
          center: 'prev,next',
          right: 'month,agendaWeek,agendaDay,today'
        }}
        eventLimit={{ agenda: 4, default: true }}
        eventSources={[
          {
            googleCalendarId: "[email protected]",
            className: 'event1'
          },
          {
            googleCalendarId: "vineyardcincinnati.com_o6jncckm5ka55fpragnbp4mk9c@group.calendar.google.com",
            className: 'event2'
          },
          {
            googleCalendarId: "[email protected]",
            className: 'event3'
          }
        ]}
        eventClick={(event) => {
          window.open(event.event.url, 'gcalevent', 'width=700,height=600');
          return false;
        }}
        loading={(bool) => {
          if (bool) {
            // Handle loading indicator if needed
          } else {
            // Hide loading indicator
          }
        }}
      />
    </div>
  );
};

export default YourCalendarComponent;

可能需要根据事件处理和加载指示器的具体要求进行调整。

© www.soinside.com 2019 - 2024. All rights reserved.