Vue2项目实战:如何基于Element UI封装一个可复用的‘批量排班’日历组件(含完整代码)
Vue2实战基于Element UI打造高复用排班日历组件在企业级后台管理系统开发中排班功能是客服、医疗、制造等行业的共性需求。本文将分享如何基于Vue2和Element UI封装一个支持批量操作、可配置班次类型的日历排班组件实现跨项目复用。1. 组件设计思路与核心功能排班组件的核心价值在于将复杂的排班逻辑封装成简单易用的接口。我们设计的组件需要满足以下业务场景可视化排班日历视图直观展示每日班次安排批量操作支持按日期范围批量设置班次灵活配置班次类型、颜色样式可自定义数据绑定与业务系统无缝集成关键设计决策采用Element UI的Calendar组件作为基础使用slot插槽自定义日期单元格内容通过props暴露配置接口事件机制通知排班变更2. 组件封装实现2.1 基础结构搭建首先创建ScheduleCalendar.vue组件文件template div classschedule-container el-calendar v-modelcurrentDate template slotdateCell slot-scope{date, data} div classdate-cell !-- 自定义日期单元格内容 -- /div /template /el-calendar /div /template script export default { name: ScheduleCalendar, props: { scheduleData: { type: Object, default: () ({}) } }, data() { return { currentDate: new Date() } } } /script2.2 排班数据渲染在日期单元格中渲染排班信息template slotdateCell slot-scope{date, data} div classdate-cell div classdate-header {{ data.day.split(-).slice(1).join(-) }} /div div classschedule-items div v-for(item, index) in getScheduleItems(data.day) :keyindex classschedule-item :classshift-${item.type} i :classgetShiftIcon(item.type)/i span{{ item.group }}/span /div /div /div /template配套的JavaScript逻辑methods: { getScheduleItems(date) { return this.scheduleData[date] || [] }, getShiftIcon(type) { const icons { morning: el-icon-sunrise-1, afternoon: el-icon-sunny, night: el-icon-moon } return icons[type] || el-icon-time } }3. 功能扩展实现3.1 批量排班功能添加批量排班抽屉组件el-drawer title批量排班 :visible.syncbatchDrawerVisible size40% el-form :modelbatchForm el-form-item label排班日期 label-width80px el-date-picker v-modelbatchForm.dateRange typedaterange value-formatyyyy-MM-dd range-separator至 start-placeholder开始日期 end-placeholder结束日期 /el-date-picker /el-form-item el-form-item label班次设置 label-width80px div v-for(shift, index) in batchForm.shifts :keyindex el-select v-modelshift.type placeholder班次类型 el-option v-foritem in shiftTypes :keyitem.value :labelitem.label :valueitem.value /el-option /el-select el-button clickremoveShift(index)删除/el-button /div el-button clickaddShift添加班次/el-button /el-form-item /el-form div classdrawer-footer el-button clickbatchDrawerVisible false取消/el-button el-button typeprimary clickconfirmBatch确定/el-button /div /el-drawer3.2 单日排班编辑实现点击日期单元格编辑单日排班methods: { handleDateClick(date, data) { this.currentEditDate data.day this.editDrawerVisible true this.editForm.shifts JSON.parse(JSON.stringify( this.getScheduleItems(data.day) || [] )) }, saveDailySchedule() { this.$emit(schedule-change, { date: this.currentEditDate, shifts: this.editForm.shifts }) this.editDrawerVisible false } }4. 组件API设计与配置项4.1 Props配置props: { // 排班数据 { 2023-10-01: [{type: morning, group: A组}] } scheduleData: { type: Object, default: () ({}) }, // 可选班次类型配置 shiftTypes: { type: Array, default: () [ { value: morning, label: 早班, color: #d9ffd9 }, { value: afternoon, label: 中班, color: #fff0bd }, { value: night, label: 晚班, color: #ddeffb } ] }, // 是否允许拖拽调整顺序 draggable: { type: Boolean, default: true } }4.2 事件机制组件通过以下事件与父组件通信schedule-change排班数据变更时触发date-click点击日期单元格时触发// 在需要通知父组件的地方 this.$emit(schedule-change, { date: 2023-10-01, shifts: [...] })5. 样式定制与优化5.1 基础样式设置.date-cell { height: 100%; display: flex; flex-direction: column; padding: 5px; } .date-header { font-size: 12px; margin-bottom: 5px; } .schedule-items { flex: 1; overflow-y: auto; } .schedule-item { margin: 3px 0; padding: 2px 5px; border-radius: 3px; font-size: 12px; display: flex; align-items: center; } .shift-morning { background-color: #d9ffd9; color: #11be11; } .shift-afternoon { background-color: #fff0bd; color: #fccb2c; } .shift-night { background-color: #ddeffb; color: #2dabff; }5.2 响应式适配media screen and (max-width: 768px) { .date-header { font-size: 10px; } .schedule-item { font-size: 10px; padding: 1px 3px; } }6. 组件使用示例6.1 基本使用template schedule-calendar :schedule-datascheduleData schedule-changehandleScheduleChange / /template script import ScheduleCalendar from ./components/ScheduleCalendar.vue export default { components: { ScheduleCalendar }, data() { return { scheduleData: { 2023-10-01: [ { type: morning, group: A组 }, { type: afternoon, group: B组 } ] } } }, methods: { handleScheduleChange({date, shifts}) { this.$set(this.scheduleData, date, shifts) } } } /script6.2 自定义班次类型schedule-calendar :schedule-datascheduleData :shift-typescustomShiftTypes / script export default { data() { return { customShiftTypes: [ { value: day, label: 白班, color: #ffffff }, { value: night, label: 夜班, color: #000000 } ] } } } /script7. 性能优化建议虚拟滚动当排班数据量很大时考虑实现虚拟滚动数据分片加载按月或周加载排班数据防抖处理批量操作时添加防抖本地缓存临时保存未提交的排班数据// 示例使用lodash的防抖函数 import { debounce } from lodash methods: { confirmBatch: debounce(function() { // 批量保存逻辑 }, 500) }8. 常见问题解决方案8.1 日期格式处理推荐使用moment.js或day.js处理日期import moment from moment // 生成日期范围数组 function getDateRange(start, end) { const dates [] let current moment(start) const last moment(end) while (current last) { dates.push(current.format(YYYY-MM-DD)) current current.clone().add(1, days) } return dates }8.2 数据持久化提供数据导入导出功能// 导出为JSON export function exportSchedule(data) { const blob new Blob([JSON.stringify(data)], { type: application/json }) const url URL.createObjectURL(blob) const link document.createElement(a) link.href url link.download schedule-${moment().format(YYYYMMDD)}.json link.click() } // 从JSON导入 export function importSchedule(file) { return new Promise((resolve, reject) { const reader new FileReader() reader.onload e { try { resolve(JSON.parse(e.target.result)) } catch (err) { reject(err) } } reader.readAsText(file) }) }9. 组件测试策略9.1 单元测试重点// 示例测试用例 describe(ScheduleCalendar, () { it(应正确渲染排班数据, () { const wrapper mount(ScheduleCalendar, { propsData: { scheduleData: { 2023-10-01: [{ type: morning, group: A组 }] } } }) expect(wrapper.find(.shift-morning).exists()).toBe(true) }) it(应触发schedule-change事件, async () { const wrapper mount(ScheduleCalendar) wrapper.vm.handleDateClick(new Date(), { day: 2023-10-01 }) await wrapper.vm.$nextTick() expect(wrapper.emitted(date-click)).toBeTruthy() }) })9.2 E2E测试场景日历渲染正确性验证批量排班功能测试单日编辑功能测试数据持久化测试10. 组件文档规范为组件编写使用文档# ScheduleCalendar 排班日历组件 ## 功能概述 提供可视化排班界面支持批量操作和单日编辑 ## 基本用法 vue template schedule-calendar :schedule-datascheduleData / /template ## API ### Props | 参数 | 说明 | 类型 | 默认值 | |------|------|-----|-------| | scheduleData | 排班数据 | Object | {} | | shiftTypes | 班次类型配置 | Array | [...] | ### Events | 事件名 | 说明 | 回调参数 | |-------|------|---------| | schedule-change | 排班变更时触发 | {date, shifts} |在实际项目中这个排班组件已经成功应用于医院护士排班、客服人员排班等多个场景通过合理的抽象和配置减少了80%的重复开发工作。组件的关键在于平衡通用性和灵活性既提供开箱即用的功能又保留足够的定制空间。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2555987.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!