vue自定义日历

留置自己用的,封装日历组件方便自定义样式。

go 复制代码
<template>
	<view style="padding: 30rpx;">
		<view class="calendar-container">
			<!-- 日历标题 -->
			<view class="calendar-header flex align-center justify-center">
				<image @click="prevMonth" mode="widthFix" src="@/static/cw_client_provider/img/calendar_left.png"
					style="width: 13rpx; height: 12rpx;"></image>
				<text style="margin: 0 14rpx;display: inline-block;">{{ year }}年{{ month }}月</text>
				<image @click="nextMonth" mode="widthFix" src="@/static/cw_client_provider/img/calendar_right.png"
					style="width: 13rpx; height: 12rpx;"></image>
			</view>
			<!-- 星期显示 -->
			<view class="calendar-week">
				<text v-for="(dayOfWeek, index) in dayOfWeeks" :key="index">{{ dayOfWeek }}</text>
			</view>
			<!-- 日期及上班休息状态显示 -->
			<view class="calendar-days">
				<view v-for="(day, index) in days" :key="index" class="calendar-day"
					:class="{ 'is-weekend': isWeekend(day), 'has-work-status': day.workStatus }">
					<!-- 自定义排版 -->
					<!-- :class="day.workStatusNew === 'work'?'calendar-day-work':'calendar-day-xiu'" -->
					<!-- 法定节假日 -->
					<!-- :class="{ 'is-weekend': isWeekend(day), 'has-work-status': day.workStatus }" -->
					<text
						:class="day.workStatusNew === 'work'?'calendar-day-text':'calendar-day-xiu'">{{ day.dateNew }}</text>
					<text v-if="day.workStatusNew">{{ day.workStatusNew === 'work'? '上班' : '休息' }}</text>
				</view>
			</view>
		</view>
		<!-- <image mode="widthFix" src="@/static/cw_client_provider/img/calendar_bg.png"
			style="width:626rpx; height: 62rpx;"></image>
		<view class="calendar-paiban">

		</view> -->

	</view>
</template>

<script>
	export default {
		name: 'hi-picker-calendar',
		props: {
			show: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				year: 2023,
				month: 6,
				dayOfWeeks: ['日', '一', '二', '三', '四', '五', '六'],
				days: [],
				prevIcon: '←',
				nextIcon: '→'
			};
		},
		watch: {
			show(newVal, oldVal) {
				console.log('show----newVal', newVal, 'show----oldVal', oldVal)
				if (newVal) {
					this.generateCalendar();
				}

			}
		},
		onLoad() {
			console.log('onLoad', this.show)
		},
		onShow() {
			console.log('onShow', this.show)
		},
		mounted() {
			console.log('mounted', this.show)
			if (this.show) {
				this.generateCalendar();
			}
		},
		methods: {
			// 生成日历数据
			generateCalendar() {
				const firstDay = new Date(this.year, this.month - 1, 1);
				const lastDay = new Date(this.year, this.month, 0);
				const firstDayOfWeek = firstDay.getDay();
				const daysInMonth = lastDay.getDate();
				let dayIndex = 0;
				for (let i = 0; i < firstDayOfWeek; i++) {
					this.days.push({
						date: '',
						workStatus: null
					});
					dayIndex++;
				}
				// for (let i = 1; i <= daysInMonth; i++) {
				//   const workStatus = this.getWorkStatus(i);
				//   this.days.push({ date: i, workStatus });
				//   dayIndex++;
				// }
				for (let i = 1; i <= daysInMonth; i++) {
					const workStatus = this.getWorkStatus(i);
					const workStatusNew = this.getWorkStatusN(i);
					const dateObj = new Date(this.year, this.month - 1, i);
					this.days.push({
						dateNew: i,
						date: dateObj,
						workStatus,
						workStatusNew
					});
					dayIndex++;
				}

				while (dayIndex % 7 !== 0) {
					this.days.push({
						date: '',
						workStatus: null
					});
					dayIndex++;
				}
				console.log('this.days', this.days)
			},
			// 自定义获取上班休息状态的方法
			getWorkStatus(day) {
				// 这里可以根据业务逻辑自定义返回上班还是休息,示例数据如下
				const workRestData = {
					3: 'rest',
					4: 'rest',
					6: 'rest',
					10: 'rest',
					17: 'rest',
					18: 'rest',
					21: 'rest',
					24: 'rest'
				};
				return workRestData[day] || 'work';
			},
			getWorkStatusN(day) {
				if (day == 1 || day == 2 || day == 8 || day == 9) {
					return 'work';
				}
				return 'rest'
			},
			// 判断是否为周末
			isWeekend(day) {
				console.log('day', day)
				return day.date && (day.date.getDay() === 0 || day.date.getDay() === 6);
			},
			// 上一个月
			prevMonth() {
				this.month--;
				if (this.month === 0) {
					this.month = 12;
					this.year--;
				}
				this.days = [];
				this.generateCalendar();
			},
			// 下一个月
			nextMonth() {
				this.month++;
				if (this.month === 13) {
					this.month = 1;
					this.year++;
				}
				this.days = [];
				this.generateCalendar();
			}
		}
	};
</script>

<style scoped>
	.calendar-container {
		width: 100%;
		background-color: #fff;
		border-radius: 26rpx;
		padding-top: 16rpx;
	}

	.calendar-header {
		align-items: center;
		margin-bottom: 30rpx;
		font-weight: 500;
		font-size: 28rpx;
		color: #333333;
		line-height: 40rpx;
	}

	.calendar-week {
		display: flex;
		justify-content: space-around;
		margin-bottom: 25rpx;
		font-weight: 500;
		font-size: 26rpx;
		color: #333333;
	}

	.calendar-days {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;

	}

	.calendar-day {
		display: flex;
		justify-content: center;
		flex-direction: column;
		width: 14.28%;
		text-align: center;
		font-weight: 500;
		font-size: 24rpx;
		color: #333333;
		margin-bottom: 26rpx;
	}

	.calendar-day.is-weekend {
		color: red;
	}

	.calendar-day.has-work-status {
		position: relative;
	}

	.calendar-day.has-work-status::after {
		content: attr(data-work-status);
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		font-size: 12px;
	}

	.calendar-day-xiu {
		/* color: #4172F0 !important; */
	}

	.calendar-day-work {
		color: #82868D;
	}

	.calendar-day-text {
		color: #333333;
	}

	.calendar-paiban {
		width: 100%;
		background-color: #fff;
		padding: 36rpx;
		border-radius: 26rpx;
	}
</style>
相关推荐
console.log('npc')7 分钟前
partial在react接口定义中是什么意思
前端·javascript·typescript
SuperEugene8 分钟前
前端 utils 工具函数规范:拆分 / 命名 / 复用全指南,避开全局污染等高频坑|编码语法规范篇
开发语言·前端·javascript
C澒11 分钟前
微前端容器标准化 —— 公共能力篇:通用请求
前端·架构
llxxyy卢20 分钟前
web部分中等题目
android·前端
若惜29 分钟前
selenium自动化测试web自动化测试 框架封装Pom
前端·python·selenium
Amumu1213842 分钟前
Js:内置对象
开发语言·前端·javascript
广州华水科技44 分钟前
2026年单北斗GNSS变形监测系统推荐,助力精准监控与智慧城市建设
前端
鸡吃丸子1 小时前
如何编写一个高质量的AI Skill
前端·ai
我命由我123451 小时前
Element Plus 2.2.27 的单选框 Radio 组件,选中一个选项后,全部选项都变为选中状态
开发语言·前端·javascript·html·ecmascript·html5·js
Luna-player1 小时前
第3章 Spring Boot的Web应用支持,个人学习笔记
前端·spring boot·学习