当前位置: 首页 > news >正文

FullCalendar Timeline View 使用

FullCalendar  Timeline View(v4)

The Scheduler add-on provides a new view called “timeline view” with a customizable horizontal time-axis and resources as rows.

1. 先安装fullcalendar和用到的插件

npm install --save @fullcalendar/core @fullcalendar/resource-timeline @fullcalendar/interaction

2.在使用时导入

import {Calendar} from '@fullcalendar/core';
import resourceTimelinePlugin from '@fullcalendar/resource-timeline';
import interactionPlugin from '@fullcalendar/interaction';
import '@fullcalendar/core/main.css';
import '@fullcalendar/timeline/main.css';
import '@fullcalendar/resource-timeline/main.css';

3. 初始化Calendar时,添加使用的插件

 plugins: [interactionPlugin, resourceTimelinePlugin],

4.最终实现资源/事件的添加删除.

 

 

上代码.

  1 import {Calendar} from '@fullcalendar/core';
  2 import resourceTimelinePlugin from '@fullcalendar/resource-timeline';
  3 import interactionPlugin from '@fullcalendar/interaction';
  4 import '@fullcalendar/core/main.css';
  5 import '@fullcalendar/timeline/main.css';
  6 import '@fullcalendar/resource-timeline/main.css';
  7 
  8 // import zh_cn from '@fullcalendar/core/locales/zh-cn';
  9 let option = {
 10     schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
 11     plugins: [interactionPlugin, resourceTimelinePlugin],
 12     defaultView: 'resourceTimeline',
 13     now: '2019-03-07',
 14     // locale: zh_cn,
 15     selectable: true,
 16     selectHelper: true,
 17     editable: true, // enable draggable events
 18     eventResourceEditable: false,
 19     aspectRatio: 1,
 20     // height: 440,
 21     contentHeight: 440,
 22     resourceAreaWidth: '120px',
 23     eventOverlap: false,
 24     selectOverlap: false,
 25     eventTextColor: '#fff',
 26     displayEventTime: true,
 27     displayEventEnd: true,
 28     slotLabelFormat: {
 29         hour: 'numeric',
 30         minute: '2-digit',
 31         omitZeroMinute: true,
 32         meridiem: 'short',
 33         hour12: false,
 34     },
 35     eventTimeFormat: {
 36         hour: 'numeric',
 37         minute: '2-digit',
 38         meridiem: 'narrow',
 39         hour12: false,
 40     },
 41     header: {
 42         left: '',
 43         center: '',
 44         right: '',
 45     },
 46     resourceLabelText: '姓名',
 47     resources: [],
 48     events: [],
 49 };
 50 /**
 51  * {Object} option , onSelect: function onEventClick: function ,
 52  */
 53 
 54 class Timeline {
 55     constructor(el, opt = {}, callBack = () => {}) {
 56         this.callBack = callBack;
 57         console.log('timeline -init');
 58         this._option = Object.assign(
 59             {
 60                 select: info => this.select(info),
 61                 dateClick: info => this.dateClick(info),
 62                 eventClick: info => this.eventClick(info),
 63                 eventMouseEnter: info => this.eventMouseEnter(info),
 64                 eventMouseLeave: info => this.eventMouseLeave(info),
 65                 eventResize: info => this.eventResize(info),
 66                 eventDrop: info => this.eventDrop(info),
 67                 resourceRender: info => this.resourceRender(info),
 68                 eventRender: info => this.eventRender(info),
 69                 eventDestroy: info => this.eventDestroy(info),
 70             },
 71             option,
 72             opt
 73         );
 74         console.log('timeline-option==>>', this._option);
 75         this.calendar = new Calendar(el, this._option);
 76         this.render();
 77 
 78         let currentDate = new Date(this._option.now);
 79         let end = new Date().setDate(currentDate.getDate());
 80         if (currentDate.getHours() !== 0) {
 81             end = new Date().setDate(currentDate.getDate() + 1);
 82         }
 83         console.table('start, end', currentDate, new Date(end));
 84         this.setOption('visibleRange', {
 85             start: currentDate,
 86             end: end,
 87         });
 88     }
 89 
 90     /**
 91      * @param {Object} value
 92      */
 93     setOption(key, value) {
 94         this.calendar.setOption(key, value);
 95         this._option[key] = value;
 96     }
 97 
 98     // methods
 99     render() {
100         this.calendar.render();
101     }
102     addResource(resource) {
103         if (!resource) {
104             return;
105         }
106         this.calendar.addResource(resource);
107     }
108     removeResource(resource, e) {
109         if (!this._option.editable) {
110             return;
111         }
112         this._option.onRemoveResource && this._option.onRemoveResource(resource);
113         let events = resource.getEvents();
114         events.forEach(event => {
115             event.remove();
116         });
117         resource.remove();
118         this.getResult();
119 
120         e.target.removeEventListener('click', this.removeResource);
121     }
122     addEvent(event) {
123         if (!event) {
124             return;
125         }
126         let tmp = this.calendar.getEventById(event.id);
127         if (tmp) {
128             for (let key in event) {
129                 if (tmp.extendedProps[key]) {
130                     tmp.setExtendedProp(key, event[key]);
131                     continue;
132                 }
133                 if (tmp[key]) {
134                     tmp.setProp(key, event[key]);
135                 }
136             }
137         } else {
138             this.calendar.addEvent(event);
139             console.log('addd', event);
140         }
141     }
142     removeEvent(eventId) {
143         let event = this.calendar.getEventById(eventId);
144         if (event) {
145             event.remove();
146             this.getResult();
147         }
148     }
149 
150     destroy() {
151         this.calendar.destroy();
152         console.log('timeline destroy >>>>>>>');
153     }
154     getResult() {
155         let resources = this.calendar.getResources();
156         let result = [];
157         resources.map(item => {
158             let tmp = {
159                 resource: item,
160                 events: item.getEvents(),
161             };
162             result.push(tmp);
163         });
164 
165         this.callBack && this.callBack(result);
166     }
167     isValid(event) {
168         let now = this._option.now;
169         let start = new Date(event.start).getTime();
170         let end = new Date(event.end).getTime();
171         let startH = new Date(now).getHours();
172         let startD = new Date(now).getDate();
173         let crossDate = new Date(now);
174         crossDate.setDate(startD);
175         crossDate.setHours(23);
176         let endPoint = crossDate.getTime();
177         if (startH !== 0) {
178             crossDate.setDate(startD + 1);
179             crossDate.setHours(startH);
180             endPoint = crossDate.getTime();
181         }
182         if (start < now || end < now || start > endPoint || end > endPoint) {
183             return false;
184         }
185         return true;
186     }
187     /**
188         callbacks 
189      */
190     select(info) {
191         if (!this.isValid({start: info.start, end: info.end})) {
192             // info.revert();
193             return;
194         }
195         this._option.onSelect && this._option.onSelect(info);
196     }
197     dateClick(arg) {
198         console.log('dateClick', arg.date, arg.resource ? arg.resource.id : '(no resource)');
199     }
200     eventClick(info) {
201         this._option.onEventClick && this._option.onEventClick(info);
202     }
203     eventMouseEnter(info) {
204         this._option.onEventMouseEnter && this._option.onEventMouseEnter(info);
205     }
206     eventMouseLeave(info) {
207         this._option.onEventMouseLeave && this._option.onEventMouseLeave(info);
208     }
209     eventResize(info) {
210         if (!this.isValid(info.event)) {
211             info.revert();
212         }
213         // this.getResult();
214     }
215     eventDrop(info) {
216         if (!this.isValid(info.event)) {
217             info.revert();
218         }
219         // this.getResult();
220     }
221     resourceRender(info) {
222         let dom = info.el;
223         dom.style = dom.style + ';position:relative;';
224         let close = document.createElement('i');
225         close.classList.add('iconfont', 'icon-c');
226         close.style = 'position:absolute;right:10px;top:50%;transform:translateY(-50%);font-size: 10px;';
227         close.addEventListener('click', e => this.removeResource(info.resource, e));
228         dom.appendChild(close);
229     }
230     eventRender(info) {
231         this.getResult();
232     }
233 
234     eventDestroy(info) {
235         // this.getResult();
236         // console.log('eventDestroy', info);
237     }
238 }
239 
240 export default Timeline;
View Code

 使用(示例)

 let timelineView = new Timeline(
     document.querySelector('#time-line-day'), {
         now: new Date(),
         onSelect: info => {
             let event = {
                 id: this.eventId,
                 title: this.eventId,
                 resourceId: info.resource.id,
                 start: info.start,
                 end: info.end,
             };
             timelineView.addEvent(event);

         },
         onRemoveResource: info => {

         },
         onEventClick: info => {
             let event = {
                 id: info.event.id,
                 title: info.event.title,
                 resourceId: info.event.getResources()[0].id,
                 start: info.event.start,
                 end: info.event.end,
             };
             let resourceId = info.event.getResources()[0].id;

         },
     },
     result => {

     }
 );
View Code

 

 

 

 FullCalendar插件 plug-index

转载于:https://www.cnblogs.com/bzhy/p/10784879.html

相关文章:

  • 08-图9 关键活动 (30 分)
  • Numpy用户指南
  • 涨姿势:抛弃字母、数字和下划线写SHELL
  • c++实现扫描检测硬件改动
  • 百度地图API获取数据
  • leetcode 338. 比特位计数(Counting Bits)
  • 2019-04-30vmware虚拟机安装macos 10.8格式为iso
  • 【Python爬虫】听说你又闹书荒了?豆瓣读书9.0分书籍陪你过五一
  • Player Settings-Web
  • c++11多线程笔记
  • 微软UWP应用,导航栏设计。
  • Python 之继承
  • 写给我即将出生小孩的第一封信
  • Centos6.5安装Redis3.2.8
  • SSH connect issue 'exec request failed on channel 0'
  • interface和setter,getter
  • Java编程基础24——递归练习
  • Laravel5.4 Queues队列学习
  • maven工程打包jar以及java jar命令的classpath使用
  • Mybatis初体验
  • MySQL数据库运维之数据恢复
  • PHP CLI应用的调试原理
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 百度小程序遇到的问题
  • 浅析微信支付:申请退款、退款回调接口、查询退款
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 学习JavaScript数据结构与算法 — 树
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (2)Java 简介
  • (2.2w字)前端单元测试之Jest详解篇
  • (Git) gitignore基础使用
  • (WSI分类)WSI分类文献小综述 2024
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (多级缓存)缓存同步
  • (学习日记)2024.01.09
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
  • (转)http协议
  • .net 微服务 服务保护 自动重试 Polly
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • .NET文档生成工具ADB使用图文教程
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • @RequestParam,@RequestBody和@PathVariable 区别
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • [AutoSar]BSW_Memory_Stack_003 NVM与APP的显式和隐式同步
  • [C#]C# winform实现imagecaption图像生成描述图文描述生成
  • [CareerCup] 13.1 Print Last K Lines 打印最后K行
  • [Enterprise Library]调用Enterprise Library时出现的错误事件之关闭办法
  • [Hive] 常见函数
  • [IDF]摩斯密码
  • [JavaWeb学习] idea新建web项目
  • [jQuery]div滚动条回到最底部