FullCalendar Timeline View 使用

来源:https://www.cnblogs.com/bzhy/archive/2019/04/28/10784879.html
-Advertisement-
Play Games

FullCalendar Timeline View(v4) The Scheduler add-on provides a new view called “timeline view” with a customizable horizontal time-axis and resources ...


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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、前言 在之前的前端開發中,為了實現我們的需求,通常採用的方案是通過 JS/Jquery 直接操縱頁面的 DOM 元素,得益於 Jquery 對於 DOM 元素優異的操作能力,我們可以很輕易的對獲取到的 DOM 元素進行操作。但是,當我們開始在前端項目中使用 Vue 這類的 MVVM 框架之後,對 ...
  • 學習筆記 1.jQuery動畫的淡入淡出 2.jQuery廣告彈窗 ...
  • Code Splitting是webpack的一個重要特性,他允許你將代碼打包生成多個bundle。對多頁應用來說,它是必須的,因為必須要配置多個入口生成多個bundle;對於單頁應用來說,如果只打包成一個bundle可能體積很大,導致無法利用瀏覽器並行下載的能力,且白屏時間長,也會導致下載很多可能... ...
  • 一、rem佈局基本原理 原理:rem可以理解為一個長度單位,單位rem的值等於網頁font-size的值。如果網頁的字體大小為預設值16px,那麼1rem就等於16px,0.5rem等於8px。 根據這個原理,如果網頁預設的字體大小改變,那麼單位rem的大小也會改變,我們使用rem做單位的HTML元 ...
  • 近期由於業務的需求,讓我這從未想過要碰Web Gis的業餘前端開發者,走了Web Gis的開發道路。功能需求很簡單,但卻也是讓自己難為了好幾天。如,應該選擇那個Gis框架,Gis框架的相容性如何,直接Ie哪些版,能不能簡單到只有一張圖片就行解決問題,等等。。。。。。 在如此多的技術盲點,以及不確定的 ...
  • this一般運用場景: 1.位於函數中,誰調用指向誰 var make = "Mclaren"; var model = "720s" function fullName() { console.log(this.make + " " + this.model); } var car = { mak ...
  • 因為IT互聯網發展的非常迅速,而web前端這塊很火,目前工資水平給的很高,在市場上也是非常的稀缺人才,現在各個行業轉行做web前端的很多,今天給大家一些建議,希望新手少走點彎路吧! 建議一:有一個比較適合自己系統的學習方案,系統的學習教程,很多人在開始學習web前端的時候都不知道如何規劃,也不知道w ...
  • Node.js安裝和簡單使用 1. 安裝方法 簡單的安裝方式是直接官網下載,然後本地安裝即可。官網地址:nodejs.org Windows系統下,選擇和系統版本匹配的.msi尾碼的安裝文件。Mac OS X系統下,選擇.pkg尾碼的安裝文件。 2. 測試是否安裝成功 打開終端,鍵入命令 ,如果進入 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...