是否可以删除EventSources并根据“视图名称”将其动态添加回去?

时间:2019-04-05 20:00:37

标签: javascript fullcalendar fullcalendar-4

我正在尝试设置一个日历,其中包含多个视图,除了每个视图上显示的事件外,所有视图均相同。我可以通过viewSkeleton Render回调按ID删除EventSources,但是无法将EventSource添加回日历中。

广泛搜索了切换可见性和类似方法的方法-但大多数结果是针对FullCalendar的早期版本的-试图在v4中实现这一目标。

精简测试案例: https://codepen.io/jacobcboe/pen/MReJep

我实际上是在尝试使用Google Calendar EventSources进行此操作,但是为了简单起见,使用数组事件作为测试用例。

scale
ggplot(data=data,aes (x=y, y=x, color = z)) + 
  coord_flip() +
  geom_point(pch = 16, size = 3, position = position_dodge(width = 0.2)) +
  xlab("ID") +
  ylab("Time") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))
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();
});

0 个答案:

没有答案