美文网首页
Vue实例:医院统一信息平台(封装fullcalendar组件)

Vue实例:医院统一信息平台(封装fullcalendar组件)

作者: 碧波之心 | 来源:发表于2018-06-28 18:25 被阅读5059次

前言

fullcalendar是一款用来管理日程安排、工作计划的日历工具,它功能非常强大而且非常实用。官方地址:https://fullcalendar.io/
它是基于jquery,jquery-ui的组件。在网上也找过这个组件的vue封装。但是觉得太不到我的需要。自己封装一个吧。

效果图

效果图一
效果图二

组件目录

目录结构

calendar.vue是fullcalendar的封装,draggable.vue是jquery-ui的拖动组件的封装。第二个是为了实现外部手动event到日历中。

安装基础组件

fullcalendar基于jquery。需要先加入项目,根据官方文档安装需要的就行了。

npm install jquery fullcalendar fullcalendar-scheduler -S

因为组件中用到了fullcalendar-scheduler,所以一并安装了。

代码

calendar.vue

<script>
import 'fullcalendar/dist/fullcalendar.min.css'
import 'fullcalendar-scheduler/dist/scheduler.min.css'
import 'fullcalendar'
import 'fullcalendar-scheduler'
import $ from 'jquery'
import _ from 'lodash'
export default {
  name: 'VueFullcalendar',
  props: {
    options: {
      type: Object,
      default () {
        return {
        }
      }
    },
    header: {
      type: Object,
      default () {
        return {
          left: 'prev,next today',
          center: 'title',
          right: 'month,agendaWeek,agendaDay'
        }
      }
    },
    resources: {
      type: Array,
      default () {
        return []
      }
    },
    events: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      calendar: null,
      instance: null,
      currentOptions: this.options
    }
  },
  computed: {
    defaultOptions () {
      const self = this
      return {
        schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives',
        locale: 'zh-cn',
        editable: true,
        selectable: true,
        droppable: true,
        viewRender (...args) {
          self.$emit('viewRender', ...args)
        },
        viewDestroy (...args) {
          self.$emit('viewDestroy', ...args)
        },
        eventDragStart (...args) {
          self.$emit('eventDragStart', ...args)
        },
        eventDragStop (...args) {
          self.$emit('eventDragStop', ...args)
        },
        eventDrop (...args) {
          self.$emit('eventDrop', ...args)
        },
        drop (...args) {
          let removeElement = _.isUndefined(self.currentOptions.deleteDroppableElement) ? true : self.currentOptions.deleteDroppableElement
          if (removeElement) {
            $(this).remove()
          }
          self.$emit('drop', ...args)
        },
        eventReceive (event) {
          self.$emit('eventReceive', event)
        },
        eventResizeStart (...args) {
          self.$emit('eventResizeStart', ...args)
        },
        eventResizeStop (...args) {
          self.$emit('eventResizeStop', ...args)
        },
        eventResize (...args) {
          self.$emit('eventResize', ...args)
        },
        eventClick (...args) {
          self.$emit('eventClick', ...args)
        },
        eventMouseover (...args) {
          self.$emit('eventMouseover', ...args)
        },
        eventMouseout (...args) {
          self.$emit('eventMouseout', ...args)
        },
        eventAfterRender (...args) {
          self.$emit('eventAfterRender', ...args)
        },
        eventAfterAllRender (...args) {
          self.$emit('eventAfterAllRender', ...args)
        },
        eventRender (...args) {
          self.$emit('eventRender', ...args)
        },
        eventDestroy (...args) {
          self.$emit('eventDestroy', ...args)
        },
        resourceRender (...args) {
          self.$emit('resourceRender', ...args)
        }
      }
    }
  },
  created: function () {
  },
  mounted: function () {
    this.$nextTick(function () {
      this.renderCalendar()
    })
  },
  beforeDestroy () {
    this.calendar = null
    this.instance = null
  },
  watch: {
    'options': {
      deep: true,
      handler (val) {
        this.changeOptions()
      }
    },
    'resources': {
      deep: true,
      handler (val) {
        this.changeResources()
      }
    },
    'events': {
      deep: true,
      handler (val) {
        this.changeEvents()
      }
    }
  },
  methods: {
    getInstance () {
      return this.instance
    },
    changeEvents () {
      let self = this
      if (self.calendar) {
        $(this.$el).fullCalendar('removeEvents')
        $(this.$el).fullCalendar('addEventSource', self.events)
      } else {
        self.renderCalendar()
      }
    },
    changeResources () {
      this.renderCalendar()
    },
    changeOptions () {
      let self = this
      self.currentOptions = _.defaultsDeep(self.options, self.currentOptions, self.defaultOptions)
      let options = _.cloneDeep(self.currentOptions)
      if (self.calendar) {
        $(self.$el).fullCalendar('option', options)
      } else {
        self.renderCalendar()
      }
    },
    renderCalendar () {
      let self = this
      self.currentOptions = _.defaultsDeep(self.options, self.currentOptions, self.defaultOptions)
      let options = _.cloneDeep(self.currentOptions)
      if (self.calendar) {
        $(self.$el).fullCalendar('destroy')
      }
      if (self.resources && self.resources.length > 0) {
        options.resources = _.cloneDeep(self.resources)
      }
      if (self.events && self.events.length > 0) {
        options.events = _.cloneDeep(self.events)
      }
      self.calendar = $(self.$el).fullCalendar(options)
      self.instance = $(self.$el).fullCalendar('getCalendar')
    }
  }
}
</script>

<template>
  <div ref="vf"></div>
</template>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

draggable.vue

<script>
import 'jquery-ui-dist/jquery-ui.min'
import $ from 'jquery'
import _ from 'lodash'
export default {
  name: 'VueDraggable',
  props: {
    options: {
      type: Object,
      default () {
        return {
        }
      }
    },
    event: {
      type: Object,
      default () {
        return {
        }
      }
    }
  },
  computed: {
    defaultOptions () {
      return {
        revert: true,
        revertDuration: 0
      }
    }
  },
  mounted: function () {
    this.$nextTick(function () {
      this.renderDraggable()
    })
  },
  watch: {
    'event': {
      deep: true,
      handler (val) {
        $(this.$el).data('event', val)
      }
    }
  },
  methods: {
    renderDraggable: function () {
      let options = _.defaultsDeep(this.options, this.defaultOptions)
      $(this.$el).draggable(options)
      $(this.$el).data('event', this.event)
    }
  }
}
</script>

<template>
  <div ref="vf" style="cursor: pointer;">
    <slot></slot>
  </div>
</template>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

几个index.js是导出组件的,就不在这里贴代码了。

使用

组件的引入方法

import Vue from 'vue'
import VueFullcalendar from '@/components/VueFullcalendar'

Vue.use(VueFullcalendar)

下面是实例代码

<script>
import Vue from 'vue'
import VueFullcalendar from '@/components/VueFullcalendar'

Vue.use(VueFullcalendar)

export default {
  name: 'ReservationSkill',
  components: {
  },
  data () {
    return {
      options: {
        locale: 'zh-cn',
        editable: true,
        selectable: true,
        allDaySlot: false,
        defaultView: 'agendaDay',
        eventResourceField: 'room'
      },
      resources: [{
        id: 1,
        title: 'Room A'
      }, {
        id: 2,
        title: 'Room B'
      }],
      events: [{
        id: 1,
        room: 1,
        title: 'Meeting',
        start: '2018-06-28 10:00'
      }, {
        id: 2,
        room: 2,
        title: '测试',
        start: '2018-06-28 12:00'
      }]
    }
  },
  created: function () {
  },
  watch: {
  },
  methods: {
    updateOptions () {
      this.options = {
        locale: 'zh-cn',
        editable: true,
        selectable: false,
        droppable: true,
        handleWindowResize: true,
        slotEventOverlap: false,
        slotLabelFormat: 'HH:mm',
        header: false
      }
    },
    changeResources () {
      this.resources = [{
        id: 1,
        title: 'Room A'
      }, {
        id: 2,
        title: 'Room B'
      }, {
        id: 3,
        title: 'Room C'
      }, {
        id: 4,
        title: 'Room D'
      }]
    },
    changeEvents () {
      this.events = [{
        id: 1,
        room: 1,
        title: 'Meeting',
        start: '2018-06-28 10:00'
      }, {
        id: 2,
        room: 2,
        title: '测试',
        start: '2018-06-28 12:00'
      }, {
        id: 3,
        room: 3,
        title: '测试',
        start: '2018-06-28 08:00'
      }]
    },
    handleDropIn (date, jsEvent, ui, resourceId) {
      console.log(date)
      console.log(jsEvent)
      console.log(ui)
      console.log(resourceId)
    },
    handleClickEvent (calEvent, jsEvent, view) {
      console.log(calEvent)
      console.log(jsEvent)
      console.log(view)
    }
  }
}
</script>

<template>
  <el-container>
    <el-header height="45px">
      <el-form :inline="true" class="demo-form-inline" size="small">
        <el-form-item>
          <el-button type="primary" @click="updateOptions">changeOptions</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changeResources">changeResources</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changeEvents">changeEvents</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <div class="calendar-container">
        <vue-fullcalendar
          class="calendar"
          :options="options"
          :resources="resources"
          :events="events"
          @drop="handleDropIn"
          @eventClick="handleClickEvent">
        </vue-fullcalendar>
      </div>
      <div class="external-events">
        <h4>Draggable Events</h4>
        <vue-draggable class="fc-event" :event="{id: 4, title: 'My Event 1'}">My Event 1</vue-draggable>
        <vue-draggable class="fc-event" :event="{id: 5, title: 'My Event 2'}">My Event 2</vue-draggable>
        <vue-draggable class="fc-event" :event="{id: 6, title: 'My Event 3'}">My Event 3</vue-draggable>
        <vue-draggable class="fc-event" :event="{id: 7, title: 'My Event 4'}">My Event 4</vue-draggable>
        <vue-draggable class="fc-event" :event="{id: 8, title: 'My Event 5'}">My Event 5</vue-draggable>
      </div>
    </el-main>
  </el-container>
</template>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.external-events {
  position: fixed;
  z-index: 2;
  top: 120px;
  left: 20px;
  width: 150px;
  padding: 0 10px;
  border: 1px solid #ccc;
  background: #eee;
}
.calendar-container {
  position: relative;
  z-index: 1;
  margin-left: 200px;
}
.calendar {
  max-width: 900px;
  margin: 20px auto;
}
</style>

相关文章

  • Vue实例:医院统一信息平台(封装fullcalendar组件)

    前言 fullcalendar是一款用来管理日程安排、工作计划的日历工具,它功能非常强大而且非常实用。官方地址:h...

  • Vue实例:医院统一信息平台(构建项目)

    前言 前面一段时间公司里忙得很晚,睡觉时间都不太够了,就隔了几天。服务端的用户系统已经完成。现在来一点前端的内容。...

  • Vue实例:医院统一信息平台(获取数据)

    前端主要的任务是向用户获取数据或者向用户展示数据。现在我们还没有向服务器取数据,数据本身应该是存储在服务器上的。只...

  • 2018-09-18 vue第六章

    组件:组件可以扩展 HTML 元素,封装可重用的代码。组件是可复用的 Vue 实例,所以它们与 new Vue 接...

  • Vue实例:医院统一信息平台(调整路由)

    项目构建完成后,我们在对目录进行调整。把页面的骨架搭建起来。就是把前面构建的项目中,菜单规划一下,菜单点击后可以打...

  • Vue中的组件

    什么是组件? 组件可以扩展 HTML 元素,封装可重用的代码。 使用组件 注册 全局注册 局部注册通过Vue实例的...

  • 2018-09-18

    组件是可复用的 Vue 实例一、组件:可以拓展HTML元素,封装可重用的代码组件分为全局组件和局部组件通过prop...

  • SpringBoot实例:医院统一信息平台(JdbcTokenS

    前面token是保存在内存中的,用的是InMemoryTokenStore。如果想要把token保存在数据库中,这...

  • vue-5

    组件(可复用的vue实例) 注册组件必须在Vue实例化之前全局组件(跨vue实例)组件的data选项必须是一个函数...

  • vue 之组件

    vue组件可扩展html文件,封装重用代码。 1.组件注册 1.1 局部注册:只能在注册该组件的实例中使用,一处...

网友评论

      本文标题:Vue实例:医院统一信息平台(封装fullcalendar组件)

      本文链接:https://www.haomeiwen.com/subject/uwrnyftx.html