vue2实现日历12个月平铺,显示工作日休息日

本文主要是介绍vue2实现日历12个月平铺,显示工作日休息日,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考:https://blog.csdn.net/weixin_40292154/article/details/125312368
1.组件DateCalendar.vue,sass改为less

<template><div class="cc-calendar"><div class="calendar-title"><span>{{ year }}年{{ month + 1 }}月</span></div><ul class="calendar-week"><liv-for="(item, index) in calendarTitleArr":key="index"class="week-item">{{ item }}</li></ul><ul class="calendar-view"><!-- 动态设置背景颜色 --><liv-for="(item, index) in visibleCalendar":key="index"class="date-view":style="{background:handleData(item.date) && isCurrentMonth(item.date)? handleData(item.date).color: !isCurrentMonth(item.date)? '#EAF2F2': '#BFBFBF',}"><spanclass="date-day":class="[{ 'opacity-class': !isCurrentMonth(item.date) }]">{{ item.day }}</span><span class="calendar-num">{{handleData(item.date) && isCurrentMonth(item.date)? handleData(item.date).pollution: ""}}</span></li></ul><!-- <div class="color-box"><div v-for="item in colorList" :key="item.value"><span :style="{ background: item.value }"></span><span>{{ item.name }}</span></div></div> --></div>
</template><script>
import {getNewDate,getDate,formatDate,colorList,handleAQIColor,
} from "@/util.js";
export default {name: "date-calendar",props: {year: {type: [String, Number],default: 2022,},month: {type: [String, Number],default: 0,},list: {type: Array,default() {return [];},},handleClick: Function,},data() {return {calendarTitleArr: ["一", "二", "三", "四", "五", "六", "日"],colorList,};},computed: {visibleCalendar() {let calendatArr = [];let { year, month } = getNewDate(getDate(this.year, this.month, 1));let currentFirstDay = getDate(year, month, 1);// 获取当前月第一天星期几let weekDay = currentFirstDay.getDay();let startTime = null;if (weekDay == 0) {// 当月第一天是星期天startTime = currentFirstDay - 6 * 24 * 60 * 60 * 1000;} else {startTime = currentFirstDay - (weekDay - 1) * 24 * 60 * 60 * 1000;}// let monthDayNum;// 当第一天是周五 周六 周日 这个月绘制42天数据 否则为35天// if (weekDay == 5 || weekDay == 6 || weekDay == 0) {//   monthDayNum = 42;// } else {//   monthDayNum = 35;// }// for (let i = 0; i < monthDayNum; i++) {// 为了页面整齐排列 一并绘制42天for (let i = 0; i < 42; i++) {calendatArr.push({date: new Date(startTime + i * 24 * 60 * 60 * 1000),// year: year,// month: month + 1,year: new Date(startTime + i * 24 * 60 * 60 * 1000).getFullYear(),month: new Date(startTime + i * 24 * 60 * 60 * 1000).getMonth() + 1,day: new Date(startTime + i * 24 * 60 * 60 * 1000).getDate(),});}return calendatArr;},},methods: {handleData(date) {// const data = [//   {//     time: "2023-08-01",//     pollution: "工作日",//     value: "1",//   },//   {//     time: "2023-05-02",//     pollution: "休息日",//     value: "0",//   },//   // {//   //   time: "2022-03-03",//   //   pollution: "PM10",//   //   value: "123",//   // },//   // {//   //   time: "2022-05-05",//   //   pollution: "SO2",//   //   value: "186",//   // },//   // {//   //   time: "2022-09-05",//   //   pollution: "NO",//   //   value: "256",//   // },//   // {//   //   time: "2022-12-12",//   //   pollution: "CO",//   //   value: "400",//   // },// ]const data = this.list;data.forEach((item) => {if (item.pollution === "无") item.pollution = "";if (!item.value && item.value !== 0) {item.color = "#bfbfbf";return;}let list = colorList.filter((itm) => itm.name == handleAQIColor(item.value));item.color = list[0].value;});let { year, month, day } = getNewDate(date);let dateTime = year + "-" + formatDate(month + 1) + "-" + formatDate(day);let list = null;data.forEach((item) => {if (item.time == dateTime) {list = item;}});return list;},// 是否是当前月isCurrentMonth(date) {let { year: currentYear, month: currentMonth } = getNewDate(getDate(this.year, this.month, 1));let { year, month } = getNewDate(date);return currentYear == year && currentMonth == month;},},created() {},
};
</script><style lang="less" scope>
body,
ul,
ol,
li {margin: 0;padding: 0;box-sizing: border-box;
}ul,
ol {list-style: none;
}.cc-calendar {width: 441px;height: 100%;box-sizing: border-box;margin-bottom: 29px;.calendar-title {width: 100%;font-size: 16px;font-family: "Source Han Sans CN";font-weight: 500;color: #061a19;text-align: center;margin-bottom: 19px;}.calendar-week {display: flex;height: 28px;line-height: 28px;border: 1px solid #14c3ba;border-right: none;border-left: none;margin-bottom: 2px;.week-item {width: 67px;text-align: center;font-size: 14px;font-family: "Source Han Sans CN";color: #061a19;font-weight: 400;}}.calendar-view {display: flex;flex-wrap: wrap;border-top: 2px solid #e4e7ea;.date-view {width: 63px;height: 38px;border-right: 2px solid #e4e7ea;border-bottom: 2px solid #e4e7ea;box-sizing: border-box;position: relative;.date-day {padding: 8px;font-size: 12px;font-family: "Source Han Sans CN";font-weight: 400;color: #062927;}.calendar-num {position: absolute;left: 14px;bottom: 0;font-size: 14px;font-family: "Source Han Sans CN";font-weight: 400;color: #062927;}}.opacity-class {opacity: 0.5;}}.color-box {margin-top: 7px;display: flex;align-items: center;justify-content: flex-end;> div {display: flex;align-items: center;justify-content: center;> span:first-child {width: 30px;height: 14px;}> span:last-child {font-size: 14px;font-weight: 500;color: #1d2f2e;}}span {display: inline-block;width: 30px;margin-right: 1px;text-align: center;}}
}
</style>

2.util.js

export const getNewDate = (date) => {let year = date.getFullYear();let month = date.getMonth();let day = date.getDate();return {year,month,day,};};export const getDate = (year, month, day) => {return new Date(year, month, day);};export const formatDate = (date) => {date = Number(date);return date < 10 ? `0${date}` : date;};export const colorList = [{name: "工作日",value: "#74B925",},{name: "休息日",value: "#F5E31C",}];export const handleAQIColor = (value) => {// value = +value;let grade = "工作日";switch (true) {case value == "1":grade = "工作日";break;case value == "0":grade = "休息日";break;default:grade = "无";break;}return grade;};

3.父组件引用

<template><div><!-- Your template code goes here --><h1>{{ message }}</h1><button @click="changeMessage">Change Message</button><!-- 渲染12个月 --><el-dialog :visible.sync="dialogVisble" width="80%" height="400px"><div class="dialog-content"><!-- 对话框中的内容 --><!-- 如果内容较多,超出对话框高度,将会出现滚动条 --><date-calendaryear="2023":month="index"v-for="(item, index) in 12":key="index":list="pollutionData"style="float: left; margin-right: 10px"/></div></el-dialog></div>
</template><script>
import DateCalendar from "@/components/DateCalendar.vue";
export default {components: {"date-calendar": DateCalendar,},data() {return {dialogVisble: false,message: [],pollutionData: [{time: "2023-01-02",pollution: "休息日",value: "0",},{time: "2023-01-01",pollution: "工作日",value: "1",},],};},methods: {changeMessage() {this.dialogVisble = true;},},
};
</script><style scoped>
/* Your component-specific styles go here */
h1 {color: blue;
}
.dialog-content {max-height: 300px; /* 设置最大高度,超过该高度将出现滚动条 */overflow-y: auto; /* 添加垂直滚动条 */padding: 10px; /* 可选:为内容添加一些内边距,使样式更美观 */
}
</style>

效果:

样式之后再调~~
参考文章开头的链接,实现按年度进行12个月的日历平铺,并且按数组给值标记工作日和休息日。

这篇关于vue2实现日历12个月平铺,显示工作日休息日的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/606225

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库