- fullcalendarの実装
- 徒然なるままに書きます悪しからず
- jqueryを使わずに実装しました
yarn add @fullcalendar/core @fullcalendar/interaction @fullcalendar/daygrid @fullcalendar/timegrid
- カラムは予定開始のカラムとしてstart_time、予定終了のカラムとしてend_time、予定表示用のカラムとしてtitleを用意
import 'calendar'
import { Calendar } from '@fullcalendar/core';
import interactionPlugin from '@fullcalendar/interaction';
import dayGridPlugin from '@fullcalendar/daygrid';
import timeGridPlugin from '@fullcalendar/timegrid';
document.addEventListener('turbolinks:load', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new Calendar(calendarEl, {
plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
locale: 'ja',
timeZone: 'Asia/Tokyo',
headerToolbar: {
start: 'prev,next,today',
center: 'title',
end: 'dayGridMonth,timeGridWeek,timeGridDay'
},
buttonText: {
today: '今日'
},
events:'/posts.json',
// Handle the event click
eventClick: function(arg) {
var eventUrl = '/posts/' + arg.event.id; // Assuming your event has an 'id' attribute
window.location.href = eventUrl;
info.el.style.borderColor = 'red';
},
});
calendar.render();
});
def index
@posts = Post.all #投稿表示用(今回は使っていない)
@events = Post.all #カレンダー表示用(一応分けておく方が良き)
end
<div id='calendar'></div>
json.array!(@events) do |event|
json.id event.id
json.title event.title
json.start event.start_time
json.end event.end_time
end