reactjs - FullCalendar 用于创建事件的新约会按钮
问题描述
我想在标题中添加按钮,该按钮将打开我的窗口以创建新事件。我明白了,如何访问查看日历 API 来创建事件?似乎只有在日历中单击某处才能获得
const customButtons = {
newAppointment: {
text: 'New Appointment',
click: (event) => {
console.log(event);
toggleNewAppointment();
}
}
}
const headerToolbar = {
start: 'title',
center: 'newAppointment',
end: 'today prev,next',
}
解决方案
据我了解,您希望将按钮添加到标题中,以便您可以使用它来创建新事件。例如,为了实现对eventObject信息和/或addEvent方法的检查,或者使用您的自定义函数,以防您将事件存储在数据库中。
calendar = new FullCalendar.Calendar(calendarEl, {
customButtons: {
newAppointment: {
text: 'New Appointment',
click: () => {
// Here you have to create an event object, since this function has no parameter from FullCalendar API
const event = {
id: 'a',
title: 'my event',
start: '2018-09-01'
}
console.log(event);
createEventFuntion(event); // Send to your event save function
// Or use the addEvent method instead
// calendar.addEvent(event);
}
}
},
headerToolbar: {
start: 'title',
center: 'newAppointment',
end: 'today prev,next',
}
});
编辑:
反应
如果你看到这个例子,你可以处理你的事件的方式几乎和你在 Javascript 中做的一样。还可以从有关如何访问 API 的文档中查看此信息。
import React from 'react'
import FullCalendar from '@fullcalendar/react'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'
import interactionPlugin from '@fullcalendar/interaction'
export default class DemoApp extends React.Component {
// Create a reference
calendarRef = React.createRef()
state = {
weekendsVisible: true,
currentEvents: []
}
render() {
return (
<div className='demo-app'>
{this.renderSidebar()}
<div className='demo-app-main'>
<FullCalendar
ref={this.calendarRef} // Here is the reference
plugins={[dayGridPlugin, timeGridPlugin, interactionPlugin]}
headerToolbar={{
left: 'prev,next today',
center: 'newAppointment',
right: 'dayGridMonth,timeGridWeek,timeGridDay'
}}
customButtons={{
newAppointment: {
text: 'custom!',
click: function() {
alert('clicked the custom button!');
someMethod();
},
},
}}
editable={true}
selectable={true}
// ...
/* you can update a remote database when these fire:
eventAdd={function(){}}
eventChange={function(){}}
eventRemove={function(){}}
*/
/>
</div>
</div>
)
}
someMethod() {
// Then you can use the reference to access to the API
let calendarApi = this.calendarRef.current.getApi()
calendarApi.addEvent()
}
}
推荐阅读
- vue.js - 如何在 vue 3 的打字稿中定义道具类型?
- scala - Mocking circe HCursor,ACursor in scalatest
- spring-data-jpa - JPAQueryFactory NVL 函数
- rust - serde Deserialize generic 的“借用值的寿命不够长”编译器错误
- microsoft-graph-api - 微软规划师 startDate/dueDate 时区?
- mysql - MYSQL 在字符串中包含函数
- javascript - Angular 9 应用程序在处理 MQTT 消息 5 分钟后冻结
- .net-core - .NetCore 应用程序内存泄漏 - 高开销|未使用的内存
- java - Java Spark:如何在 Dataset 上做 flatMap 并为后续的 groupBy 提供新的模式?
- jquery - Ajax POST 请求 - 400 错误请求 - 格式不正确的语法