Vue+Element-ui日历排班自定义实例代码
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
前言
在日常开发需求中,可能会遇到给员工进行排班的需求,如果只是在table表格中显示,会显得枯燥、不直观,今天我们就来写一个可以自定义的日历排班功能,用的是vue2+element-ui。
效果图如下:
(图一):日历中显示排班数据,排班数据可以使用鼠标进行拖动,改变排班的顺序。
(图二):可以对日期范围进行批量、多班次排班
(图三):可以对单日排班信息进行操作,显示单日排班时间的具体信息等。
下面是代码部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | < template > < div id = "app" > < div class = "calender-class" > < div class = "batch-add-Work-class" > < el-button class = "add-btn-class" size = "small" type = "primary" @ click = "batchAddDrawer = true" >批量排班</ el-button > </ div > < el-calendar > <!-- 这里使用的是 2.5 slot 语法,对于新项目请使用 2.6 slot 语法--> < template slot = "dateCell" slot-scope = "{date, data}" > < div class = "day-content-class" > < template v-if = "viewDate[data.day]" > < div class = "header-class" > < div class = "day-class" > {{ data.day.split('-').slice(1).join('-') }} </ div > < div class = "handle-class" > < el-button icon = "el-icon-edit" size = "mini" circle @ click = "handleWorkInfo(viewDate[data.day], data)" ></ el-button > </ div > </ div > < div class = "paiban-class" > < div v-for = "(dayValue, i) in viewDate[data.day]" :class = "['draggable-div' + i, 'each-paiban-class', setWorkClass(dayValue.sort)]" draggable = "true" @ dragstart = "handleDragStart($event, dayValue, data.day)" @ dragover.prevent = "handleDragOver($event)" @ dragenter = "handleDragEnter($event, dayValue)" @ dragend = "handleDragEnd()" > < i :class = "[setIconClass(dayValue.shiftName), 'paiban-icon-class']" ></ i > < div class = "paiban-name-class" >{{ dayValue.groupName }}</ div > </ div > </ div > </ template > < template v-else> < div class = "header-class" > < div class = "day-class" > {{ data.day.split('-').slice(1).join('-') }} </ div > < div class = "handle-class" > < el-button icon = "el-icon-edit" size = "mini" circle @ click = "handleWorkInfo(viewDate[data.day], data)" ></ el-button > </ div > </ div > < div class = "no-work-class" > < div class = "icon-class" >< i class = "el-icon-date" ></ i ></ div > < div class = "tips-class" > 暂无排班 </ div > </ div > </ template > </ div > </ template > </ el-calendar > </ div > <!-- 批量排班抽屉弹窗 --> < div > < el-drawer title = "批量排班" :visible.sync = "batchAddDrawer" size = "40%" > < div class = "demo-drawer__content" > < el-form :model = "batchAddForm" > < el-form-item label = "排班日期" label-width = "80px" > < el-date-picker v-model = "batchAddForm.batchDate" value-format = "yyyy-MM-dd" type = "daterange" range-separator = "至" start-placeholder = "开始日期" end-placeholder = "结束日期" > </ el-date-picker > </ el-form-item > < el-button type = "primary" icon = "el-icon-plus" circle @ click = "addDomain" ></ el-button > < el-form-item label-width = "80px" v-for = "(data, index) in batchAddForm.classData" :label = "'排班' + (index + 1) + ':'" :key = "data.key" > < p ></ p > < span >班次:</ span > < el-radio-group v-model = "data.shiftName" > < el-radio label = "早" >早</ el-radio > < el-radio label = "中" >中</ el-radio > < el-radio label = "晚" >晚</ el-radio > </ el-radio-group > < p ></ p > < span >班别:</ span > < el-radio-group class = "margin-left:80px" v-model = "data.groupName" > < el-radio label = "甲" >甲</ el-radio > < el-radio label = "乙" >乙</ el-radio > < el-radio label = "丙" >丙</ el-radio > </ el-radio-group > < el-button class = "remove-domain-class" @ click.prevent = "removeDomain(data)" type = "danger" icon = "el-icon-delete" circle></ el-button > </ el-form-item > </ el-form > </ div > < div class = "demo-drawer__footer" > < el-button @ click = "handleBatchClose" >取 消</ el-button > < el-button type = "primary" @ click = "batchAddWork()" > 确定 </ el-button > </ div > </ el-drawer > </ div > <!-- 单独排班 --> < div > < el-drawer :title = "'【' + hanleDay.day + '】排班'" :visible.sync = "drawer" size = "40%" > < div class = "add-work-class" > < el-button class = "add-btn-class" type = "primary" @ click = "innerDrawer = true" >添加</ el-button > < el-drawer title = "添加排班" :append-to-body = "true" :before-close = "handleClose" :visible.sync = "innerDrawer" > < div class = "demo-drawer__content" > < el-form :model = "addForm" > < el-form-item label = "班次:" label-width = "80px" > < el-radio-group v-model = "addForm.shiftName" > < el-radio label = "早" >早</ el-radio > < el-radio label = "中" >中</ el-radio > < el-radio label = "晚" >晚</ el-radio > </ el-radio-group > </ el-form-item > < el-form-item label = "班别:" label-width = "80px" > < el-radio-group v-model = "addForm.groupName" > < el-radio label = "甲" >甲</ el-radio > < el-radio label = "乙" >乙</ el-radio > < el-radio label = "丙" >丙</ el-radio > </ el-radio-group > </ el-form-item > </ el-form > </ div > < div class = "demo-drawer__footer" > < el-button @ click = "handleClose" >取 消</ el-button > < el-button type = "primary" @ click = "addWork()" > 确定 </ el-button > </ div > </ el-drawer > </ div > < el-table :data = "workInfoList" > < el-table-column property = "date" label = "日期" width = "100" ></ el-table-column > < el-table-column property = "shiftName" label = "班次" ></ el-table-column > < el-table-column property = "groupName" label = "班别" ></ el-table-column > < el-table-column property = "startTime" label = "开始时间" width = "160" ></ el-table-column > < el-table-column property = "endTime" label = "结束时间" width = "160" ></ el-table-column > < el-table-column fixed = "right" label = "操作" width = "120" > < template slot-scope = "scope" > < el-button @ click.native.prevent = "deleteRow(scope, workInfoList)" type = "text" size = "small" > 移除 </ el-button > </ template > </ el-table-column > </ el-table > </ el-drawer > </ div > </ div > </ template > < script > import moment from 'moment' export default { data() { return { viewDate: { "2023-06-20": [ { "id": "2023-06-20" + Math.random(1000), "ruleName": "三班两运转", "shiftName": "早", "groupName": "甲", "startTime": "2023-06-20 08:30", "endTime": "2023-06-20 20:30", "isNotHoliday": 0, "classId": 1, "date": "2023-06-20", "sort": 1 }, { "id": "2023-06-20" + Math.random(1000), "ruleName": "三班两运转", "shiftName": "中", "groupName": "乙", "startTime": "2023-06-20 20:30", "endTime": "2023-06-21 08:30", "isNotHoliday": 0, "classId": 1, "date": "2023-06-20", "sort": 2 }, ], "2023-06-21": [ { "id": "2023-06-21" + Math.random(1000), "ruleName": "三班两运转", "shiftName": "早", "groupName": "甲", "startTime": "2023-06-20 08:30", "endTime": "2023-06-20 20:30", "isNotHoliday": 0, "classId": 1, "date": "2023-06-20", "sort": 1 }, { "id": "2023-06-21" + Math.random(1000), "ruleName": "三班两运转", "shiftName": "中", "groupName": "乙", "startTime": "2023-06-20 08:30", "endTime": "2023-06-20 20:30", "isNotHoliday": 0, "classId": 1, "date": "2023-06-20", "sort": 2 }, { "id": "2023-06-21" + Math.random(1000), "ruleName": "三班两运转", "shiftName": "晚", "groupName": "丙", "startTime": "2023-06-20 08:30", "endTime": "2023-06-20 20:30", "isNotHoliday": 0, "classId": 1, "date": "2023-06-20", "sort": 3 }, ], }, thisDay: null, thisDayWork: null, ending: null, dragging: null, batchAddDrawer: false, // 批量添加 batchAddForm: { batchDate: [], classData: [{ shiftName: '早', groupName: '甲', }] }, // 单日添加 addForm: { shiftName: '早', groupName: '甲', sort: 1, }, drawer: false, innerDrawer: false, hanleDay: '', workInfoList: [] }; }, watch: { 'addForm.shiftName'(newVal, oldVal) { switch (newVal) { case '早': this.addForm.sort = 1 break; case '中': this.addForm.sort = 2 break; case '晚': this.addForm.sort = 3 break; default: break; } } }, computed: { }, methods: { handleDragStart(e, item, thisDay) { this.dragging = item this.thisDay = thisDay this.thisDayWork = this.viewDate[thisDay] }, handleDragEnd() { if (this.ending.id === this.dragging.id) { return } let newItems = [...this.thisDayWork] const src = newItems.indexOf(this.dragging) const dst = newItems.indexOf(this.ending) newItems.splice(src, 1, ...newItems.splice(dst, 1, newItems[src])) this.$set(this.viewDate, this.thisDay, newItems) this.$nextTick(() => { this.dragging = null this.ending = null }) console.log("🚀 ~ file: App.vue:286 ~ handleDragEnd ~ this.viewDate:", this.viewDate) }, handleDragOver(e) { // 首先把div变成可以放置的元素,即重写dragenter/dragover e.dataTransfer.dropEffect = 'move'// e.dataTransfer.dropEffect="move";//在dragenter中针对放置目标来设置! }, handleDragEnter(e, item) { e.dataTransfer.effectAllowed = 'move'// 为需要移动的元素设置dragstart事件 this.ending = item }, // 获取时间范围中的所有日期 enumerateDaysBetweenDates(startDate, endDate) { let daysList = []; let SDate = moment(startDate); let EDate = moment(endDate); daysList.push(SDate.format("YYYY-MM-DD")); while (SDate.add(1, "days").isBefore(EDate)) { daysList.push(SDate.format("YYYY-MM-DD")); } daysList.push(EDate.format("YYYY-MM-DD")); return daysList; }, setSortValue(value) { let sort = 1 switch (value) { case '早': sort = 1 break; case '中': sort = 2 break; case '晚': sort = 3 break; default: break; } return sort }, setWorkClass(value) { let classValue = 'no-work-class' switch (value) { case 1: classValue = 'zao-work-class' break; case 2: classValue = 'wan-work-class' break; case 3: classValue = 'ye-work-class' break; default: break; } return classValue; }, setIconClass(value) { let classValue = 'el-icon-sunrise' switch (value) { case "早": classValue = 'el-icon-sunrise' break; case "中": classValue = 'el-icon-sunny' break; case "晚": classValue = 'el-icon-moon' break; default: break; } return classValue; }, // 编辑单日排班 handleWorkInfo(info, data) { this.hanleDay = data this.drawer = true if (info && info.length > 0) { this.workInfoList = info } else { this.workInfoList = [] } }, handleClose() { this.innerDrawer = false; }, // 添加单日排班 addWork() { let info = { "id": this.hanleDay.day + Math.random(1000), "ruleName": "三班两运转", "shiftName": this.addForm.shiftName, "groupName": this.addForm.groupName, "startTime": this.hanleDay.day + " 08:30", "endTime": this.hanleDay.day + " 20:30", "isNotHoliday": 0, "classId": 1, "date": this.hanleDay.day, "sort": this.addForm.sort } this.workInfoList.push(info) this.$set(this.viewDate, this.hanleDay.day, this.workInfoList) this.$forceUpdate() this.innerDrawer = false; }, // 清除单日排班数据 deleteRow(row, tableData) { let index = row.$index tableData.splice(index, 1); if (tableData.length > 0) { this.$set(this.viewDate, this.hanleDay.day, tableData) } else { this.$delete(this.viewDate, this.hanleDay.day); } }, addDomain() { this.batchAddForm.classData.push({ shiftName: '早', groupName: '甲', key: Date.now() }); }, removeDomain(item) { if (this.batchAddForm.classData.length > 1) { var index = this.batchAddForm.classData.indexOf(item) if (index !== -1) { this.batchAddForm.classData.splice(index, 1) } } else { this.$message({ message: '请至少安排一个排班', type: 'error' }); } }, // 批量添加排班数据 batchAddWork() { let dateList = this.batchAddForm.batchDate let classList = this.batchAddForm.classData let list = [] if (dateList && dateList.length > 0) { list = this.enumerateDaysBetweenDates(dateList[0], dateList[1]) } list.forEach((item) => { let workList = [] classList.forEach((work) => { let info = { "id": item + Math.random(1000), "ruleName": "三班两运转", "shiftName": work.shiftName, "groupName": work.groupName, "startTime": item + " 08:30", "endTime": item + " 20:30", "isNotHoliday": 0, "classId": 1, "date": item, "sort": this.setSortValue(work.shiftName) } workList.push(info) }) this.$set(this.viewDate, item, workList) }) this.batchAddDrawer = false }, handleBatchClose() { this.batchAddDrawer = false } } } </ script > < style > #app { width: 100%; height: 100%; margin: 0; padding: 0; } .el-table__fixed-right { height: 100% !important; } .calender-class { width: 100%; height: 100%; } .is-selected { color: #1989FA; } .el-calendar__body { height: 85vh; } .el-calendar-table { height: 100%; } .el-calendar-day { height: 100% !important; } .day-content-class { height: 100px; display: flex; flex-direction: column; } .header-class { flex: 1; display: flex; height: 28px; flex-direction: row; justify-content: space-between; } .day-class { flex: 4; } .handle-class { flex: 1; } .paiban-class { flex: 4; display: flex; flex-direction: row; justify-content: center; align-items: flex-end; } .paiban-icon-class { font-size: 22px; margin: 8px 0 10px 0; } .paiban-name-class { padding-top: 10px; } .each-paiban-class { text-align: center; max-width: 50px; margin: 5px 5px 0 5px; border-radius: 5px; padding: 0 0 5px 0; flex: 1; } .zao-work-class { background-color: #d9ffd9; color: #11be11; } .wan-work-class { background-color: #fff0bd; color: #fccb2c; } .ye-work-class { background-color: #ddeffb; color: #2dabff; } .no-work-class { text-align: center; color: #cacaca; } .icon-class { font-size: 20px; margin-bottom: 20px; } /* 侧边弹窗 */ .add-btn-class { margin: 10px; float: right; } </ style > |
总结
到此这篇关于Vue+Element-ui日历排班的文章就介绍到这了,更多相关Vue Element-ui日历排班内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
vite打包出现 "default" is not exported by "
这篇文章主要给大家介绍了关于vite打包出现 "default" is not exported by "node_modules/...问题的解决办法,文中通过代码将解决的办法介绍的非常详细,对同样遇到这个问题的朋友具有一定的参考借鉴价值,需要的朋友可以参考下2024-06-06
最新评论