登录  /  注册
博主信息
博文 1
粉丝 0
评论 0
访问量 271
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
laravel后台极速开发框架 - 集成日历组件
eRic
原创
271人浏览过

效果截图

代码实现

  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Support\Str;
  4. class Calendar extends Widget {
  5. public static $js = [
  6. '@fullcalendar'
  7. ];
  8. public static $css = [
  9. '@fullcalendar'
  10. ];
  11. /**
  12. * @var string
  13. */
  14. protected $view = 'admin::widgets.calendar';
  15. protected $calendarId;
  16. /**
  17. * @var array
  18. */
  19. protected $items = [];
  20. protected $locale = 'zh-cn';
  21. protected $timeZone = 'Asia/Shanghai';
  22. protected $initialView = 'dayGridMonth';
  23. protected $header_toolbar_right_btn = 'dayGridMonth,timeGridWeek,timeGridDay,listWeek';
  24. /**
  25. * Collapse constructor.
  26. */
  27. public function __construct() {
  28. $this->calendarId = 'calendar' . Str::random(5);
  29. $this->id('calendar-' . uniqid());
  30. $this->class('box-group');
  31. $this->style('margin-bottom: 20px');
  32. }
  33. /**
  34. * @desc 设置 日历容器ID
  35. * @param $id
  36. */
  37. public function calendarId($id) {
  38. $this->calendarId = $id;
  39. return $this;
  40. }
  41. /**
  42. * @desc 设置 日历语言包
  43. * @param $value
  44. */
  45. public function locale($value) {
  46. $this->locale = $value;
  47. return $this;
  48. }
  49. /**
  50. * @desc 设置 日历的时区
  51. * @param $value
  52. */
  53. public function timeZone($value) {
  54. $this->timeZone = $value;
  55. return $this;
  56. }
  57. /**
  58. * @desc 设置 日历的初始化视图 可选值 dayGridMonth,timeGridWeek,timeGridDay,listWeek
  59. * @param $value
  60. */
  61. public function initialView($value) {
  62. $this->initialView = $value;
  63. return $this;
  64. }
  65. /**
  66. * @desc 设置 日历的头部右侧所展示的btn 全部:dayGridMonth,timeGridWeek,timeGridDay,listWeek
  67. * @param $value
  68. */
  69. public function headerToolbarRightBtn($value) {
  70. $this->header_toolbar_right_btn = $value;
  71. return $this;
  72. }
  73. /**
  74. * @desc 设置 日历的日期事件
  75. * @param $value
  76. */
  77. public function eventItem(array $item) {
  78. $this->items = $item;
  79. return $this;
  80. }
  81. /**
  82. * Add item.
  83. * 单个增加日历的日期事件
  84. * @param string $title 标题
  85. * @param string $title 描述
  86. * @param string $start 开始日期
  87. * @param string $end 结束日期
  88. * @return $this
  89. */
  90. public function addEvents($title, $description = '', $start, $end = '') {
  91. $event_info = [
  92. 'title' => $title,
  93. 'description' => $description,
  94. 'start' => $start,
  95. 'allDay' => false,
  96. 'showModal' => true,
  97. ];
  98. if (!empty($end)) {
  99. $event_info['end'] = $end;
  100. }
  101. $this->items[] = $event_info;
  102. return $this;
  103. }
  104. //
  105. public function backgroundColor($color) {
  106. if(!empty($this->items[count($this->items) - 1])){
  107. $this->items[count($this->items) - 1]['backgroundColor'] = $color;
  108. }
  109. return $this;
  110. }
  111. public function borderColor($color) {
  112. if(!empty($this->items[count($this->items) - 1])){
  113. $this->items[count($this->items) - 1]['borderColor'] = $color;
  114. }
  115. return $this;
  116. }
  117. public function allDay($bool) {
  118. if(!empty($this->items[count($this->items) - 1])){
  119. $this->items[count($this->items) - 1]['allDay'] = $bool;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * @desc 设置 日历的日期事件是否 点击 展示modal
  125. * @param $value
  126. */
  127. public function showModal($bool = true) {
  128. if(!empty($this->items[count($this->items) - 1])){
  129. $this->items[count($this->items) - 1]['showModal'] = $bool;
  130. }
  131. return $this;
  132. }
  133. /**
  134. * @desc 设置 日历的日期事件的weburl
  135. * @param $value
  136. */
  137. public function webUrl($url) {
  138. if(!empty($this->items[count($this->items) - 1])){
  139. $this->items[count($this->items) - 1]['url'] = $url;
  140. }
  141. return $this;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function defaultVariables() {
  147. return [
  148. 'id' => $this->id,
  149. 'locale' => $this->locale,
  150. 'timeZone' => $this->timeZone,
  151. 'initialView' => $this->initialView,
  152. 'header_toolbar_right_btn' => $this->header_toolbar_right_btn,
  153. 'calendar_id' => $this->calendarId,
  154. 'items' => json_encode($this->items, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
  155. 'attributes' => $this->formatAttributes(),
  156. ];
  157. }
  158. }

对应的前端模板

  1. <div class="calendar-box">
  2. <div id="{{$calendar_id}}"></div>
  3. <div id="external-events">
  4. </div>
  5. <div id="drop-remove">
  6. </div>
  7. <!-- 模态框 -->
  8. <div class="modal fade" id="{{$calendar_id}}-Modal" tabindex="-1" data-backdrop="static" role="dialog" aria-labelledby="eventModalLabel" aria-hidden="true">
  9. <div class="modal-dialog modal-lg" role="document">
  10. <div class="modal-content ">
  11. <div class="modal-header">
  12. <h5 class="modal-title">事件详情</h5>
  13. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  14. <span aria-hidden="true">&times;</span>
  15. </button>
  16. </div>
  17. <div class="modal-body">
  18. </div>
  19. <div class="modal-footer">
  20. <button type="button" class="btn btn-secondary" data-dismiss="modal">知道了</button>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </div>
  26. <script>
  27. /* initialize the external events
  28. -----------------------------------------------------------------*/
  29. function ini_events(ele) {
  30. ele.each(function () {
  31. // create an Event Object (https://fullcalendar.io/docs/event-object)
  32. // it doesn't need to have a start or end
  33. var eventObject = {
  34. title: $.trim($(this).text()) // use the element's text as the event title
  35. }
  36. // store the Event Object in the DOM element so we can get to it later
  37. $(this).data('eventObject', eventObject)
  38. // make the event draggable using jQuery UI
  39. $(this).draggable({
  40. zIndex: 1070,
  41. revert: true, // will cause the event to go back to its
  42. revertDuration: 0 // original position after the drag
  43. })
  44. })
  45. }
  46. ini_events($('#external-events div.external-event'))
  47. /* initialize the calendar
  48. -----------------------------------------------------------------*/
  49. //Date for the calendar events (dummy data)
  50. var date = new Date()
  51. var d = date.getDate(),
  52. m = date.getMonth(),
  53. y = date.getFullYear()
  54. var Calendar = FullCalendar.Calendar;
  55. var Draggable = FullCalendar.Draggable;
  56. var containerEl = document.getElementById('external-events');
  57. var checkbox = document.getElementById('drop-remove');
  58. var calendarEl = document.getElementById('{{$calendar_id}}');
  59. // initialize the external events
  60. // ----------------------------------------------------------------
  61. new Draggable(containerEl, {
  62. itemSelector: '.external-event',
  63. eventData: function (eventEl) {
  64. return {
  65. title: eventEl.innerText,
  66. backgroundColor: window.getComputedStyle(eventEl, null).getPropertyValue('background-color'),
  67. borderColor: window.getComputedStyle(eventEl, null).getPropertyValue('background-color'),
  68. textColor: window.getComputedStyle(eventEl, null).getPropertyValue('color'),
  69. };
  70. }
  71. });
  72. var calendar = new Calendar(calendarEl, {
  73. initialView: '{{$initialView}}',
  74. selectable: true,
  75. locale: '{{$locale}}',
  76. timeZone: '{{$timeZone}}',
  77. headerToolbar: {
  78. left: 'prev,next today',
  79. center: 'title',
  80. right: '{{$header_toolbar_right_btn}}'
  81. },
  82. buttonText: { // 设置按钮文本为中文
  83. today: '今天',
  84. month: '月',
  85. week: '周',
  86. day: '日',
  87. list: '日程',
  88. },
  89. views: {
  90. list: {
  91. buttonText: '日程',
  92. }
  93. },
  94. themeSystem: 'bootstrap',
  95. events: {!! $items !!},
  96. eventClick: function(info) {
  97. // 检查事件是否允许弹出模态框
  98. if (info.event.extendedProps.showModal) {
  99. // 获取事件的标题和描述
  100. var eventTitle = info.event.title;
  101. var eventDescription = info.event.extendedProps.description;
  102. // 设置模态框的内容
  103. $('#{{$calendar_id}}-Modal').find('.modal-title').text(eventTitle);
  104. $('#{{$calendar_id}}-Modal').find('.modal-body').text(eventDescription);
  105. // 显示模态框
  106. $('#{{$calendar_id}}-Modal').modal('show');
  107. }
  108. },
  109. editable: true,
  110. droppable: false, // this allows things to be dropped onto the calendar !!!
  111. drop: function (info) {
  112. // is the "remove after drop" checkbox checked?
  113. if (checkbox.checked) {
  114. // if so, remove the element from the "Draggable Events" list
  115. info.draggedEl.parentNode.removeChild(info.draggedEl);
  116. }
  117. }
  118. });
  119. calendar.render();
  120. // $('#calendar').fullCalendar()
  121. /* ADDING EVENTS */
  122. var currColor = '#3c8dbc' //Red by default
  123. // Color chooser button
  124. $('#color-chooser > li > a').click(function (e) {
  125. e.preventDefault()
  126. // Save color
  127. currColor = $(this).css('color')
  128. // Add color effect to button
  129. $('#add-new-event').css({
  130. 'background-color': currColor,
  131. 'border-color': currColor
  132. })
  133. })
  134. $('#add-new-event').click(function (e) {
  135. e.preventDefault()
  136. // Get value and make sure it is not null
  137. var val = $('#new-event').val()
  138. if (val.length == 0) {
  139. return
  140. }
  141. // Create events
  142. var event = $('<div />')
  143. event.css({
  144. 'background-color': currColor,
  145. 'border-color': currColor,
  146. 'color': '#fff'
  147. }).addClass('external-event')
  148. event.text(val)
  149. $('#external-events').prepend(event)
  150. // Add draggable funtionality
  151. ini_events(event)
  152. // Remove event from text input
  153. $('#new-event').val('')
  154. })
  155. </script>

使用方法

  1. <?php
  2. use Dcat\Admin\Widgets\Calendar;
  3. // 获取实例
  4. $calendar = Calendar::make();
  5. $events_list = [
  6. [
  7. 'title' => '今天的事项',// 事项标题
  8. 'start' => '2024-10-01 14:00:00', // 事项开始日期,您可以根据需要替换
  9. 'end' => '2024-10-02 16:00:00', // 事项结束日期
  10. 'backgroundColor' => '#f56954', // red
  11. 'borderColor' => '#f56954', // red
  12. 'allDay' => false, // 指示事件是否为全天事件。默认为 false
  13. 'description' => '这是今天的事项描述', // 描述信息
  14. 'showModal' => true, // 是否弹出 事项详情 modal
  15. // 'url' => 'https://www.baidu.com/', // 指定跳转url
  16. ]
  17. ];
  18. $calendar->eventItem($events_list); // 给定日期事项列表

配置项

  1. <?php
  2. // 获取实例
  3. $calendar = Calendar::make();
  4. // 设置 日历容器ID
  5. $calendar->calendarId('calendar_ids');
  6. // 设置 日历语言包 默认 zh-cn
  7. $calendar->locale('zh-cn');
  8. // 设置 日历的时区 默认 Asia/Shanghai
  9. $calendar->timeZone('Asia/Shanghai');
  10. // 设置 日历的初始化视图 默认 dayGridMonth 可选值 dayGridMonth,timeGridWeek,timeGridDay,listWeek
  11. $calendar->initialView('dayGridMonth');
  12. // 设置 日历的头部右侧所展示的btn 可选值:dayGridMonth,timeGridWeek,timeGridDay,listWeek 。可全部
  13. $calendar->headerToolbarRightBtn('dayGridMonth');

plus 版已集成,可直接使用

  1. composer require dcat-plus/laravel-admin:1.2.8

plus 版已集成多种表单组件

表单:sku 来自于 :https://github.com/abbotton/dcat-sku-plus
表单:多媒体选择器 来自于:https://github.com/deatil/dcat-form-media
在此感谢他们开源对dcat-admin 的贡献。



dcat-admin 并未停止不前,plus版 持续更新 保持活力

dcat-plus admin (plus版)沿用 dcat-admn 最新代码,并让dcat-admin 保持活力。已支持到Laravel11,并新增多个组件。

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学