Search code examples
javascriptvue.jsfullcalendar-4

How add remove function to event delete button in FullCalendarV4 and Vue.js


I am building an interactive calendar with FullCalendarV4 and Vue.js. I successfully leveraged the eventRender callback function to append a delete button to each dynamically created event. The delete button still needs functionality. In order to enable deleting of events, I attempted to use the .setAttribute method to append an onclick attribute to the button to trigger a "remove" function targeting individual events "btn.setAttribute("onclick", 'remove(info)'); Then, I tried using a .removeChild method inside the remove function enable deleting, but to no avail. Any recommendations on how to add functionality to the dynamically created delete button? 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"
      @eventRender="eventRender"
      @eventClick="remove"
      />
  </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(info) {
      var btn = document.createElement("button");
      btn.appendChild(document.createTextNode("x"));
      btn.setAttribute("onclick", 'remove(info)');
      info.el.appendChild(btn);
    },
    remove(info) {
      var task = this.event.currentTarget.parentNode;
      info.el.removeChild(task);
    }
  }
}
</script>

<style lang='scss'>
@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

  • Can you try changing

      btn.setAttribute("onclick", 'remove(info)');
    

    to

    btn.addEventListener('click', () => {
        info.el.remove()
    })