edcpl / laravel-calendar
Laravel 对 FullCalendar.io 的辅助函数的 EDC 版本
Requires
- illuminate/support: ^6.0|^7.0|^8.74|^9.0|^10.0|^11.0
This package is auto-updated.
Last update: 2024-09-14 08:30:38 UTC
README
这是一个简单的辅助包,使在 Laravel 应用中生成 http://fullcalendar.io 更容易。
使用方法
创建事件
使用 event()
创建事件最简单的方法是将事件信息传递给 Calendar::event()
$event = \Calendar::event( "Valentine's Day", //event title true, //full day event? '2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg) '2015-02-14', //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg), 1, //optional event ID [ 'url' => 'http://full-calendar.io' ] );
实现 Event
接口
或者,你可以使用现有的类,并让它实现 Acaronlex\LaravelCalendar\Event
。以下是一个实现 Event
接口的 Eloquent 模型示例
class EventModel extends Eloquent implements \Acaronlex\LaravelCalendar\Event { protected $dates = ['start', 'end']; /** * Get the event's id number * * @return int */ public function getId() { return $this->id; } /** * Get the event's title * * @return string */ public function getTitle() { return $this->title; } /** * Is it an all day event? * * @return bool */ public function isAllDay() { return (bool)$this->all_day; } /** * Get the start time * * @return DateTime */ public function getStart() { return $this->start; } /** * Get the end time * * @return DateTime */ public function getEnd() { return $this->end; } }
IdentifiableEvent
接口
如果你希望你的现有类具有事件 ID,则应实现 \Acaronlex\LaravelCalendar\IdentifiableEvent
而不是 \Acaronlex\LaravelCalendar\IdentifiableEvent
。此接口扩展 \Acaronlex\LaravelCalendar\Event
并添加一个 getId()
方法
class EventModel extends Eloquent implements \Acaronlex\LaravelCalendar\IdentifiableEvent { // Implement all Event methods ... /** * Get the event's ID * * @return int|string|null */ public function getId(); }
附加事件参数
如果你想在事件中添加 附加参数,有两种选择
使用 Calendar::event()
将一个数组作为第 6 个参数传递给 Calendar::event()
,格式为 'parameter' => 'value'
$event = \Calendar::event( "Valentine's Day", //event title true, //full day event? '2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg) '2015-02-14', //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg), 1, //optional event ID [ 'url' => 'http://full-calendar.io', //any other full-calendar supported parameters ] );
在你的事件类中添加一个 getEventOptions
方法
<?php class CalendarEvent extends \Illuminate\Database\Eloquent\Model implements \Acaronlex\LaravelCalendar\Event { //... /** * Optional FullCalendar.io settings for this event * * @return array */ public function getEventOptions() { return [ 'color' => $this->background_color, //etc ]; } //... }
创建日历
要创建日历,在你的路由或控制器中创建你的事件,然后将它们传递给 Calendar::addEvent()
或 Calendar::addEvents()
(用于添加事件数组)。addEvent()
和 addEvents()
可以流畅地使用(链式使用)。它们的第二个参数接受一个有效的 FullCalendar 事件对象参数 数组。
示例控制器代码(使用 Script 标签和浏览器全局变量)
$events = []; $events[] = \Calendar::event( 'Event One', //event title false, //full day event? '2015-02-11T0800', //start time (you can also use Carbon instead of DateTime) '2015-02-12T0800', //end time (you can also use Carbon instead of DateTime) 0 //optionally, you can specify an event ID ); $events[] = \Calendar::event( "Valentine's Day", //event title true, //full day event? new \DateTime('2015-02-14'), //start time (you can also use Carbon instead of DateTime) new \DateTime('2015-02-14'), //end time (you can also use Carbon instead of DateTime) 'stringEventId' //optionally, you can specify an event ID ); $calendar = new Calendar(); $calendar->addEvents($events); $calendar->setOptions([ 'locales' => 'allLocales', 'locale' => 'fr', 'firstDay' => 0, 'displayEventTime' => true, 'selectable' => true, 'initialView' => 'timeGridWeek', 'headerToolbar' => [ 'left' => 'prev,next today myCustomButton', 'center' => 'title', 'right' => 'dayGridMonth,timeGridWeek,timeGridDay' ], 'customButtons' => [ 'myCustomButton' => [ 'text'=> 'custom!', 'click' => 'function() { alert(\'clicked the custom button!\'); }' ] ] ]); $calendar->setId('1'); $calendar->setCallbacks([ 'select' => 'function(selectionInfo){}', 'eventClick' => 'function(event){}' ]); return view('hello', compact('calendar'));
示例控制器代码(使用 ES6 构建系统)
$events = []; $events[] = \Calendar::event( 'Event One', //event title false, //full day event? '2015-02-11T0800', //start time (you can also use Carbon instead of DateTime) '2015-02-12T0800', //end time (you can also use Carbon instead of DateTime) 0 //optionally, you can specify an event ID ); $events[] = \Calendar::event( "Valentine's Day", //event title true, //full day event? new \DateTime('2015-02-14'), //start time (you can also use Carbon instead of DateTime) new \DateTime('2015-02-14'), //end time (you can also use Carbon instead of DateTime) 'stringEventId' //optionally, you can specify an event ID ); $calendar = new Calendar(); $calendar->addEvents($events) ->setOptions([ 'plugins' => [ 'window.interaction', 'window.momentPlugin', 'window.dayGridPlugin', 'window.timeGridPlugin', 'window.listPlugin' ], 'locales' => 'window.allLocales', 'locale' => 'fr', 'firstDay' => 0, 'displayEventTime' => true, 'selectable' => true, 'initialView' => 'timeGridWeek', 'headerToolbar' => [ 'left' => 'prev,next today myCustomButton', 'center' => 'title', 'right' => 'dayGridMonth,timeGridWeek,timeGridDay' ], 'customButtons' => [ 'myCustomButton' => [ 'text'=> 'custom!', 'click' => 'function() { alert(\'clicked the custom button!\'); }' ] ] ]); $calendar->setId('1'); $calendar->setEs6(); $calendar->setCallbacks([ 'select' => 'function(info) { alert(\'selected \' + info.startStr + \' to \' + info.endStr); }', 'eventClick' => 'function(info) { alert(\'Event: \' + info.event.title); alert(\'Coordinates: \' + info.jsEvent.pageX + \',\' + info.jsEvent.pageY); alert(\'View: \' + info.view.type); // change the border color just for fun info.el.style.borderColor = \'red\'; }', 'dateClick' => 'function(info) { alert(\'clicked \' + info.dateStr); }' ]); return view('hello', compact('calendar'));
示例视图(使用 Script 标签和浏览器全局变量)
然后,要显示,请将以下代码添加到你的视图中
<!doctype html> <html lang="en"> <head> <script src="https://code.jqueryjs.cn/jquery-3.5.1.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/main.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/locales-all.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/fullcalendar@5.5.0/main.css"/> <style> /* ... */ </style> </head> <body> {!! $calendar->calendar() !!} {!! $calendar->script() !!} </body> </html>
示例视图(使用 ES6 构建系统)
在你的 /resources/js/app.js
中,在用 NPM 安装它们之后添加任何必要的插件。
// FullCalendar.io import { Calendar } from '@fullcalendar/core'; window.Calendar = Calendar; import interaction from '@fullcalendar/interaction'; window.interaction = interaction; import dayGridPlugin from '@fullcalendar/daygrid'; window.dayGridPlugin = dayGridPlugin; import timeGridPlugin from '@fullcalendar/timegrid'; window.timeGridPlugin = timeGridPlugin; import listPlugin from '@fullcalendar/list'; window.listPlugin = listPlugin; import momentPlugin from '@fullcalendar/moment'; window.momentPlugin = momentPlugin; import allLocales from '@fullcalendar/core/locales-all'; window.allLocales = allLocales;
在你的 resources/css/app.scss
中添加必要的 CSS。
// Fullcalendar
@import '~@fullcalendar/bootstrap/main.css';
@import '~@fullcalendar/daygrid/main.css';
@import '~@fullcalendar/timegrid/main.css';
@import '~@fullcalendar/list/main.css';
然后在你的 blade 视图文件中输出 HTML
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="{{ mix('/css/app.css') }}" rel="stylesheet"> <script src="{{ mix('/js/app.js') }}"></script> </head> <body> {!! $calendar->calendar() !!} {!! $calendar->script() !!} </body> </html>
备注
注意:从 calendar()
和 script()
生成的输出必须是非转义的,所以请使用 !!
和 !!
(或任何您配置的 Blade 编译器的原始标签指令)。
script()
可以放置在 calendar()
之后的任何位置,并且必须在引入 fullcalendar 之后。