Search code examples
javascriptvue.jsfullcalendar-4

How to remove FullCalendar-4 events in Vue.js


I am attempting to build an interactive calendar using FullCalendarV4 and Vue.js. So far, I have set up the code to enable event inputs using a prompt method in a handleDateClick function. I want to append a functional delete button to each event. The documentation says that this can be done using eventRender callback. I tried using this callback to append a button but to no avail. Any recommendations on how to make this work? My code is below. Thanks!

<template>
  <div class='demo-app'>
    <FullCalendar
      class='demo-app-calendar'
      ref="fullCalendar"
      defaultView="dayGridMonth"
      :header="{
        left: 'prev,next today',
        center: 'title',
        right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
       }"
      :plugins="calendarPlugins"
      :weekends="calendarWeekends"
      :events="calendarEvents"
      @dateClick="handleDateClick"
      />
  </div>
</template>

<script>
import FullCalendar from '@fullcalendar/vue'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'
import interactionPlugin from '@fullcalendar/interaction'

export default {
  components: {
    FullCalendar // make the <FullCalendar> tag available
  },
  data: function() {
    return {
      calendarPlugins: [ // plugins must be defined in the JS
        dayGridPlugin,
        timeGridPlugin,
        interactionPlugin // needed for dateClick
      ],
      calendarWeekends: true,
      calendarEvents: []
    }
  },
  methods: {
    handleDateClick(arg) {
    var newTitle = prompt(arg.dateStr);
      if (newTitle != null) {
        this.calendarEvents.push({
          title: newTitle,
          start: arg.date,
          allDay: arg.allDay
        })
      }
    },
    eventRender: function(event){
      var btn = document.createElement("button");
      btn.appendChild(document.createTextNode("x"));
      event.appendChild(btn);
    }
  }
}
</script>

<style lang='scss'>

// you must include each plugins' css
// paths prefixed with ~ signify node_modules
@import '~@fullcalendar/core/main.css';
@import '~@fullcalendar/daygrid/main.css';
@import '~@fullcalendar/timegrid/main.css';

.demo-app {
  font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
  font-size: 14px;
}

.demo-app-top {
  margin: 0 0 3em;
}

.demo-app-calendar {
  margin: 0 auto;
  max-width: 900px;
}

</style>


Solution

  • You need to bind @eventRender method to FullCalendar component To access element in eventRender, you need to use event.el. You can check the document here

       <FullCalendar
              class='demo-app-calendar'
              ref="fullCalendar"
              defaultView="dayGridMonth"
              :header="{
                left: 'prev,next today',
                center: 'title',
                right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
              }"
              :plugins="calendarPlugins"
              :weekends="calendarWeekends"
              :events="calendarEvents"
              @dateClick="handleDateClick"
              @eventRender="eventRender"
        />
    
        methods: {
            eventRender(info) {
              var btn = document.createElement("button");
              btn.appendChild(document.createTextNode("x"));
              info.el.appendChild(btn);
            }
        }