Is it possible to Remove EventSources and add them back dynamically based on View Name?

不问归期 提交于 2021-02-10 14:16:27

问题


I'm trying to set up a calendar with multiple views that are all identical except for the events displayed on each. I am able to remove EventSources by ID through the viewSkeleton Render callback - but I am unable to add an EventSource back to the calendar.

Searched extensively for ways to toggle visibility and things like that - but most results are for previous versions of FullCalendar - trying to accomplish this in v4.

Reduced Test Case: https://codepen.io/jacobcboe/pen/MReJep

I'm actually trying to do this with Google Calendar EventSources, but used array events for test case for simplicity.

document.addEventListener('DOMContentLoaded', function() {
  var calendarEl = document.getElementById('calendar');

  var calendar = new FullCalendar.Calendar(calendarEl, {
    plugins: [ 'timeGrid' ],
    views: {
	    view1: {
			type: 'timeGrid',
			duration: { days:7 },
			buttonText: 'view1'
		},
		view2: {
			type: 'timeGrid',
			duration: { days:7 },
			buttonText: 'view2'
		},
		view3: {
		    type: 'timeGrid',
			duration: { days:7 },
			buttonText: 'view3'
		},
		view4: {
			type: 'timeGrid',
			duration: { days:7 },
			buttonText: 'view4'
		}			
	},
    defaultView: 'view1',
    defaultDate: '2019-04-05',
    header: {
      left: 'view1,view2,view3,view4',
      center: '',
      right: ''
    },
    eventSources: [
      {
        id:'1',
        events: [
        {
          title: 'All Day Event',
          start: '2019-04-05'
        },
        {
          title: 'Long Event',
          start: '2019-04-06',
          end: '2019-04-07'
        }]
      },
      {
        id:'2',
        events:[
          {
            groupId: '999',
            title: 'Repeating Event',
            start: '2019-04-05T16:00:00'
          },
          {
            groupId: '999',
            title: 'Repeating Event',
            start: '2019-04-06T16:00:00'
          },
          {
            title: 'Conference',
            start: '2019-04-05',
            end: '2019-04-10'
          }]
      },
      {
        id:'3',
        events: [
          {
        title: 'Meeting',
        start: '2019-04-09T10:30:00',
        end: '2019-04-09T12:30:00'
      },
      {
        title: 'Lunch',
        start: '2019-04-08T12:00:00'
      },
      {
        title: 'Meeting',
        start: '2019-04-12T14:30:00'
      },
      {
        title: 'Birthday Party',
        start: '2019-04-07T07:00:00'
      },
      {
        title: 'Click for Google',
        url: 'http://google.com/',
        start: '2019-04-11'
      }]
      }
    ],
    viewSkeletonRender: function(info) {
      var eventSourceOne = calendar.getEventSourceById('1');
      eventSourceOne.remove();
      var eventSourceTwo = calendar.getEventSourceById('2');
      eventSourceTwo.remove();
      var eventSourceThree = calendar.getEventSourceById('3');
      eventSourceThree.remove();
      //also tried calendar.getEvents() with remove
      var viewName = info.view
      if(viewName.type =='view1'){
        calendar.addEventSource(eventSourceOne);
        //have also tried eventSourceOne.refetch()
      }
      if(viewName.type =='view2'){
        calendar.addEventSource(eventSourceTwo);
      }
      if(viewName.type =='view3'){
        calendar.addEventSource(eventSourceThree);
      }
      if(viewName.type =='view4'){
        calendar.addEventSource(eventSourceOne);
        calendar.addEventSource(eventSourceTwo);
        calendar.addEventSource(eventSourceThree);
      }
    }
  });

  calendar.render();
});
html, body {
  margin: 0;
  padding: 0;
  font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
  font-size: 14px;
}

#calendar {
  max-width: 900px;
  margin: 40px auto;
}
<script src="https://fullcalendar.io/releases/timegrid/4.0.1/main.min.js"></script>
<script src="https://fullcalendar.io/releases/daygrid/4.0.1/main.min.js"></script>
<script src="https://fullcalendar.io/releases/interaction/4.0.2/main.min.js"></script>
<script src="https://fullcalendar.io/releases/core/4.0.2/main.min.js"></script>
<link href="https://fullcalendar.io/releases/timegrid/4.0.1/main.min.css" rel="stylesheet"/>
<link href="https://fullcalendar.io/releases/daygrid/4.0.1/main.min.css" rel="stylesheet"/>
<link href="https://fullcalendar.io/releases/core/4.0.2/main.min.css" rel="stylesheet"/>
<div id='calendar'></div>

来源:https://stackoverflow.com/questions/55542592/is-it-possible-to-remove-eventsources-and-add-them-back-dynamically-based-on-vie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!