当前位置:首页 > VUE

vue实现日历人员值班

2026-01-19 23:15:39VUE

实现日历人员值班功能

日历人员值班功能可以通过Vue结合第三方日历组件或自定义实现。以下是一种基于FullCalendar的实现方案。

安装依赖

需要安装@fullcalendar/vue及相关插件:

vue实现日历人员值班

npm install @fullcalendar/vue @fullcalendar/core @fullcalendar/daygrid @fullcalendar/timegrid @fullcalendar/interaction

基本组件结构

创建一个Vue组件来承载日历功能:

<template>
  <FullCalendar :options="calendarOptions" />
</template>

<script>
import FullCalendar from '@fullcalendar/vue'
import dayGridPlugin from '@fullcalendar/daygrid'
import timeGridPlugin from '@fullcalendar/timegrid'
import interactionPlugin from '@fullcalendar/interaction'

export default {
  components: { FullCalendar },
  data() {
    return {
      calendarOptions: {
        plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin],
        initialView: 'dayGridMonth',
        headerToolbar: {
          left: 'prev,next today',
          center: 'title',
          right: 'dayGridMonth,timeGridWeek'
        },
        events: [
          {
            title: '张三值班',
            start: '2023-11-01',
            end: '2023-11-02',
            color: '#ff9f89'
          },
          {
            title: '李四值班',
            start: '2023-11-05',
            end: '2023-11-07',
            color: '#f6c3d0'
          }
        ],
        editable: true,
        selectable: true,
        dateClick: this.handleDateClick,
        eventClick: this.handleEventClick
      }
    }
  },
  methods: {
    handleDateClick(arg) {
      const name = prompt('输入值班人员姓名')
      if (name) {
        this.calendarOptions.events.push({
          title: name + '值班',
          start: arg.date,
          allDay: arg.allDay,
          color: this.getRandomColor()
        })
      }
    },
    handleEventClick(info) {
      if (confirm('确定要删除这个值班安排吗?')) {
        info.event.remove()
      }
    },
    getRandomColor() {
      return '#' + Math.floor(Math.random() * 16777215).toString(16)
    }
  }
}
</script>

数据持久化

通常需要将值班数据保存到后端:

vue实现日历人员值班

methods: {
  async saveDutySchedule() {
    try {
      const response = await axios.post('/api/duty-schedule', {
        events: this.calendarOptions.events
      })
      console.log('保存成功', response.data)
    } catch (error) {
      console.error('保存失败', error)
    }
  }
}

高级功能实现

实现人员分组和批量分配:

data() {
  return {
    staffGroups: [
      { id: 1, name: '早班', members: ['张三', '李四'] },
      { id: 2, name: '晚班', members: ['王五', '赵六'] }
    ],
    selectedGroup: null
  }
},
methods: {
  assignGroupToDate(date) {
    if (!this.selectedGroup) return

    this.selectedGroup.members.forEach(member => {
      this.calendarOptions.events.push({
        title: member + '值班',
        start: date,
        color: this.getGroupColor(this.selectedGroup.id)
      })
    })
  },
  getGroupColor(groupId) {
    const colors = ['#ff9f89', '#f6c3d0', '#a4e4f9']
    return colors[groupId % colors.length]
  }
}

响应式设计优化

添加响应式处理适应不同屏幕:

computed: {
  calendarOptions() {
    return {
      ...this.baseCalendarOptions,
      headerToolbar: {
        left: window.innerWidth > 768 ? 'prev,next today' : 'prev,next',
        center: 'title',
        right: window.innerWidth > 768 ? 'dayGridMonth,timeGridWeek' : 'dayGridMonth'
      }
    }
  }
}

注意事项

  1. 生产环境需要添加数据验证和错误处理
  2. 复杂场景可考虑使用Vuex管理状态
  3. 移动端可能需要单独优化交互方式
  4. 国际化和时区处理需要特别注意

这种实现方式提供了基本的交互功能,实际项目中可根据需求扩展更多功能如:值班历史查看、人员替换、值班统计等。

标签: 日历人员
分享给朋友:

相关文章

css日历制作

css日历制作

基础日历结构 使用HTML创建日历的基本框架,通常包含表格元素。月份、星期几和日期通过<table>标签组织,表头显示星期几,表格主体填充日期数字。 <div class="cal…

vue日历表格实现

vue日历表格实现

实现Vue日历表格的基本步骤 使用Vue实现日历表格需要处理日期数据生成、渲染和交互逻辑。以下是核心实现方法: 安装依赖(可选) 若需复杂功能可引入date-fns或dayjs等日期库: np…

vue实现日历

vue实现日历

Vue 实现日历组件 使用 Vue 实现日历组件可以通过自定义开发或借助第三方库完成。以下是两种常见方法: 方法一:自定义日历组件 创建基础日历结构,利用 Vue 的响应式特性动态渲染日期。核心逻…

vue日历实现

vue日历实现

Vue 日历组件实现方法 基础日历实现 使用第三方库如v-calendar快速搭建日历功能: npm install v-calendar 引入并注册组件: import VCalendar f…

vue实现全年日历

vue实现全年日历

实现全年日历的基本思路 使用Vue实现全年日历需要结合日期计算、数据渲染和交互逻辑。核心在于生成全年12个月的日历数据,并通过组件化方式展示。以下是具体实现方法: 安装依赖(可选) 如需更便捷的日…

vue怎么实现日历

vue怎么实现日历

实现基础日历布局 使用Vue的模板语法构建日历的HTML结构,通常采用表格形式展示日期。月份切换通过计算属性动态生成日期数组。 <template> <div class…