如何为 FullCalendar 4 和 Vue.js 中的事件删除按钮添加删除功能
How add remove function to event delete button in FullCalendarV4 and Vue.js
我正在使用 FullCalendarV4 和 Vue.js 构建交互式日历。我成功地利用 eventRender 回调函数将删除按钮附加到每个动态创建的事件。删除按钮仍然需要功能。为了能够删除事件,我尝试使用 .setAttribute 方法将 onclick 属性附加到按钮以触发针对单个事件的 "remove" 函数 "btn.setAttribute("onclick",'remove(info)') ; 然后,我尝试在 remove 函数中使用 .removeChild 方法启用删除,但无济于事。关于如何向动态创建的删除按钮添加功能的任何建议?我的代码如下。谢谢!
<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>
你能试着改变一下吗
btn.setAttribute("onclick", 'remove(info)');
至
btn.addEventListener('click', () => {
info.el.remove()
})
我正在使用 FullCalendarV4 和 Vue.js 构建交互式日历。我成功地利用 eventRender 回调函数将删除按钮附加到每个动态创建的事件。删除按钮仍然需要功能。为了能够删除事件,我尝试使用 .setAttribute 方法将 onclick 属性附加到按钮以触发针对单个事件的 "remove" 函数 "btn.setAttribute("onclick",'remove(info)') ; 然后,我尝试在 remove 函数中使用 .removeChild 方法启用删除,但无济于事。关于如何向动态创建的删除按钮添加功能的任何建议?我的代码如下。谢谢!
<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>
你能试着改变一下吗
btn.setAttribute("onclick", 'remove(info)');
至
btn.addEventListener('click', () => {
info.el.remove()
})