本文主要是介绍值班日历实现不同人显示不同的颜色区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前端UI用的移动端的vantUI。这里只是我的思路总结,和用什么UI框架关系不大。
先看效果图:
<van-calendarref="calendar":poppable="false":show-confirm="false":style="{ height: '580px' }":min-date="minDate":max-date="maxDate":default-date="new Date()":formatter="calendarFormatter"color="#dfdfdf"></van-calendar>
this.dutyArray 在获取到所有的值班人员的时候,过滤好的,里面是没有重复的值班人
this.dutyArray.forEach((it) => {if (this.dutySet.indexOf(it.dutyPerson) == -1) {this.dutySet.push(it.dutyPerson);}});
重点来了!!!!
calendarFormatter(day) {const month = day.date.getMonth() + 1;const date = (day.date.getDate() + "").padStart(2, "0");const year = day.date.getFullYear();const dateStr = `${year}-${month}-${date}`;// 当前day和dutyArray中的日期按顺序匹配,如果匹配上了,就显示值班人员const curDuty = this.dutyArray.find((it, idx) => {return it.dutyDate == dateStr;});if (curDuty) {day.bottomInfo = curDuty.dutyPerson;}// 判断当前的值班人,返回所在this.dutySet中的索引let index = this.dutySet.findIndex((it) => {return it == curDuty.dutyPerson;});if (index != -1) {if(index > 18){//防止溢出index = index % 18;}day.className = `cus-day-class${index}`;} else {day.className = `cus-day-class`;}return day;},
优雅实现css
<style lang="scss" scoped>
$colorArr: #1989fa, #6f7ad3, #f56c6c, #07c160, #e6a23c, #67c23a, #f06b49,#ecc2f1, #82c7c3, #e3698a, #d92b45, #60c9ff, #ba217d, #d2691e, #d2b48c,#ffa500, #ffc0cb, #ba55d3, #909399;@each $colorItem in $colorArr {$index: index($colorArr, $colorItem);//当前索引.van-calendar::v-deep.van-calendar__body.van-calendar__month.cus-day-class#{$index - 1}.van-calendar__bottom-info {background-color: $colorItem !important;}
}</style>
这篇关于值班日历实现不同人显示不同的颜色区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!