uni-app实现可选择日期、范围、打点的月份日历

2024-03-06 14:30

本文主要是介绍uni-app实现可选择日期、范围、打点的月份日历,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考插件市场lx-calendar插件:

地址:lx-calendar原插件地址

整体说明

1、始终标记每个月和今天相同的日期,为淡蓝色
2、月份切换为滑动切换
3、可以进行下方打点标记
4、选择日期(日期日期为多选,因功能需要不可选择今天、今天之前、非本月的日期)
(1)单击即可选中日期,选中后为深蓝色
(2)长按日期即可开启范围选择,选中范围开始日期,然后单击范围结束日期,即可选中范围,选完范围后,范围选择关闭
(3)已经选中的日期,单击可取消选中
(4)有选中日期时会出现取消选中按钮,点击即可取消所有选中日期
5、作为组件可以接收两个参数

参数作用类型示例
dot_lists打点标记数组数组[‘2022-12-09’, ‘2022-12-10’, ‘2022-12-11’]
select_lists选中的日期数组[‘2022-12-14’, ‘2022-12-15’, ‘2022-12-16’, ‘2022-12-17’, ‘2022-12-22’, ‘2022-12-27’]

6、显示效果

7、完整代码,使用的是uView的,需要重新找

<template><view class="page"><view class="card"><view class="head"><u-icon name="arrow-left"></u-icon><view class="title">{{nowYear+'年'+nowMonth+'月'}}</view><u-icon name="arrow-right"></u-icon><u-button v-show="select_list.length" @tap="cancel_select" class="cancel-btn" size="mini" type="primary">取消选择</u-button></view><view class="date_week" ><view class="week_td" v-for="(item,index) in week" :key="index">{{item}}</view></view><swiper :style="{height: '480rpx'}" :current="current" circular @change="change_date"><swiper-item><view class="date_week"><view class="week_td" :class="select_list.includes(vo.date) && vo.type == 'month' ? 'select' : ''" @tap="select(vo, index, week_list_prev)" @longtap="selectMore(vo, index)" v-for="(vo,index) in week_list_prev" :key="index"><view class="num" :class="[vo.today && vo.type == 'month' ? 'today': '',vo.type == 'month' ? 'month' : 'disabled']">{{vo.day}}</view><view v-show="vo.dot && vo.type == 'month'" class="dot"></view></view></view></swiper-item><swiper-item><view class="date_week"><view class="week_td" :class="select_list.includes(vo.date) && vo.type == 'month' ? 'select' : ''" @tap="select(vo, index, week_list)" @longtap="selectMore(vo, index)" v-for="(vo,index) in week_list" :key="index"><view class="num" :class="[vo.today && vo.type == 'month' ? 'today': '',vo.type == 'month' ? 'month' : 'disabled']">{{vo.day}}</view><view v-show="vo.dot && vo.type == 'month'" class="dot"></view></view></view></swiper-item><swiper-item><view class="date_week"><view class="week_td" :class="select_list.includes(vo.date) && vo.type == 'month' ? 'select' : ''" @tap="select(vo, index, week_list_next)" @longtap="selectMore(vo, index)" v-for="(vo,index) in week_list_next" :key="index"><view class="num" :class="[vo.today && vo.type == 'month' ? 'today': '',vo.type == 'month' ? 'month' : 'disabled']">{{vo.day}}</view><view v-show="vo.dot && vo.type == 'month'" class="dot"></view></view></view></swiper-item></swiper></view></view>
</template><script>export default {props:{select_lists:{type:Array,default:()=>['2022-12-14', '2022-12-15', '2022-12-16', '2022-12-17', '2022-12-22', '2022-12-27']},dot_lists:{type:Array,default:()=>['2022-12-09', '2022-12-10', '2022-12-11']}},data() {return {week:['日','一','二','三','四','五','六'],week_list: [],week_list_prev: [],week_list_next: [],start_date: '', // 当月的第一天end_date: '', // 当月的最后一天prev_date: '',next_date: '',nowYear: '2022',nowMonth: '12',nowDay: '08',nowTime: 0,current: 1,dot_list:[], // 打点的数组select_list: [],isMore: false,startMore: ''}},watch: {dot_lists:{immediate:true,handler(value){this.dot_list = value;this.set_doc_lists_update()}},select_lists:{immediate:true,handler(value){this.select_list = value;}}},created() {this.init();},methods: {init() {this.get_date();this.doc_list_update();this.update_month();},// 将YYYY-MM-DD变成时间戳date_parse(str){return Date.parse(str.replace(/-(\d)(?!\d)/g, '-0$1'));},select(item, index, list) {if (new Date(item.date).getTime() > new Date().getTime()) {if (item.type == 'month') {if (this.isMore) {var start = Math.min(index, this.startMore);var end = Math.max(index, this.startMore);for (var i = start + 1; i <= end; i++) {this.select_list.push(list[i].date)}this.select_list = [...new Set(this.select_list)]this.isMore = false;return}var index = this.select_list.findIndex(i => i == item.date)if (index > -1) {this.select_list.splice(index, 1)return}this.select_list.push(item.date)} else {uni.showToast({title: '只能操作本月日期~',duration: 3000,icon: 'none'});}return}uni.showToast({title: '只能操作今日之后的日期~',duration: 3000,icon: 'none'});},selectMore(item, index) {if (new Date(item.date).getTime() > new Date().getTime()) {if (item.type == 'month') {this.isMore = true;this.startMore = index;this.select_list.push(item.date)} else {uni.showToast({title: '只能在本月选择日期~',duration: 3000,icon: 'none'});}return}uni.showToast({title: '只能为今日之后的日期添加计划~',duration: 3000,icon: 'none'});},cancel_select() {this.select_list = [];this.isMore = false;this.startMore = '';},// 获得三个月的周列表get_date(value = '', type = 'same') {let date = new Date();if(value){date = new Date(value);}let nowMonth = date.getMonth() + 1,nowYear = date.getFullYear(),nowDay = date.getDate(),nowTime = date.getTime(),nowWeek = date.getDay();// 当前月有多少天let days = this.get_month_days(nowMonth,nowYear);let start_date = new Date(nowYear,nowMonth - 1, 1); // 当月的第一天let end_date = new Date(nowYear,nowMonth - 1, days); // 当月的最后一天let prev_date = new Date(start_date.getTime() - 1); // 要显示的上月的最后一天let prev_date_days = prev_date.getDate(); // 要显示的上月的最后一天的日let next_date = new Date(end_date.getTime() + 86401 * 1000); // 要显示的下月的第一天let next_date_days = next_date.getDate(); // 要显示的下月的第一天的日let start_week = start_date.getDay(); // 当月的第一天是星期几let date_arrs = [];let week_list = []; // 星期数组,一个月贡献是5周let count_days = 42; // 一页显示的天数// 将要显示的上个月的日期纳入日期数组for(let i = prev_date_days - start_week + 1; i <= prev_date_days; i++){date_arrs.push({day:i,type:'prev',today: i == new Date().getDate() ? true : false,date:`${prev_date.getFullYear()}-${prev_date.getMonth()+1 > 9 ? prev_date.getMonth()+1 : '0' + (prev_date.getMonth()+1)}-${i > 9 ? i : '0' + i}`})}// 将要显示的当月日期添加到日期数组for(let i = 1; i <= days; i++){date_arrs.push({day:i,type:'month',today: i == new Date().getDate() ? true : false,date:`${nowYear}-${nowMonth > 9 ? nowMonth : '0' + nowMonth}-${i > 9 ? i : '0' + i}`})}let date_arrs_length = date_arrs.length;// 将要显示的下个月的日期添加到日期数组中for(let i = 1; i <= count_days - date_arrs_length; i++){date_arrs.push({day:i,type:'next',today: i == new Date().getDate() ? true : false,date:`${next_date.getFullYear()}-${next_date.getMonth()+1 > 9 ? next_date.getMonth()+1 : '0' + (next_date.getMonth()+1)}-${i > 9 ? i : '0' + i}`})}// 将当月显示的42天划分为6周的周数组// for(let i = 0; i < date_arrs.length / 7; i++){// 	let arr = [];// 	for(let j = 0; j < 7; j++){// 		arr.push(date_arrs[i * 7 + j]);// 	}// 	week_list.push(arr);// }if(type == 'same'){this.week_list = date_arrs;this.nowYear = nowYear;this.nowMonth = nowMonth;this.nowDay = nowDay;this.nowTime = nowTime;this.start_date = start_date;this.end_date = end_date;this.prev_date = prev_date;this.next_date = next_date;}else if(type == 'prev'){this.week_list_prev = date_arrs;}else if(type == 'next'){this.week_list_next = date_arrs;}},// 获得当月的天数get_month_days(nowMonth,nowYear){let month_arr = [1,3,5,7,8,10,12];let days = 0;if(nowMonth == 2){if(nowYear % 4 == 0){days = 29;}else{days = 28;}}else if(month_arr.indexOf(nowMonth) >= 0){days = 31;}else{days = 30;}return days;},// 获得前一个月和后一个月要显示的日期update_month(){this.get_date(this.get_month('prev'),'prev');this.get_date(this.get_month('next'),'next');},get_month(type){let nowMonth = this.nowMonth;let nowYear = this.nowYear;let nowDay = new Date().getDate();if(type == 'prev'){if(nowMonth == 1){nowMonth = 12;nowYear = Number(nowYear) - 1;}else{nowMonth--;}}else if(type == 'next'){if(nowMonth == 12){nowMonth = 1;nowYear = Number(nowYear) + 1;}else{nowMonth++;}}return this.date_parse(`${nowYear}-${nowMonth}-${nowDay}`);},// 滑动切换获得天数change_date_month(type){let week_list = this.week_list;if(type == 'prev'){this.week_list = this.week_list_prevthis.week_list_prev = this.week_list_next;this.week_list_next = week_list;}else if(type == 'next'){this.week_list = this.week_list_nextthis.week_list_next = this.week_list_prev;this.week_list_prev = week_list;}},// 滑动切换change_date(e){let primary_current = this.current; // 之前显示的swiper-itemlet current = e.detail.current; // 切换后的swiper-itemthis.current = current; // 更新swiper-item的显示if(primary_current - current == -1 || primary_current - current == 2){this.get_date(this.get_month('next'));this.update_month();if(primary_current - current == -1 && current != 1){this.change_date_month('prev')}else if(primary_current - current == 2){this.change_date_month('next')}}else{this.get_date(this.get_month('prev'));this.update_month();if(primary_current - current == 1 && current != 1){this.change_date_month('next')}else if(primary_current - current == -2){this.change_date_month('prev')}}this.set_doc_lists_update();},// 设置打点set_doc_lists_update(){this.doc_list_update('week_list');this.doc_list_update('week_list_prev');this.doc_list_update('week_list_next');},// 为当前月设置打点doc_list_update(week_list = 'week_list'){let list = [];// item为一周的数组this[week_list].forEach((vo,key)=>{// vo为一天的对象if(this.dot_list.indexOf(vo.date) > -1 || this.dot_list.indexOf(vo.date.replace(/-(\d)(?!\d)/g, '-0$1')) > -1 ){vo.dot = true;}else{vo.dot = false;}list.push(vo)})this[week_list] = list;},}}
</script><style lang="scss">@import '@/uview-ui/index.scss';.page {width: 100vw;height: 100vh;background: #F1F1F1;box-sizing: border-box;padding-top: 10rpx;}.card {width: 720rpx;margin: 10rpx auto;padding: 10rpx;box-sizing: border-box;background: #fff;border-radius: 10rpx;box-shadow: 0 0 10rpx #C0C0C0;.head {position: relative;height: 80rpx;display: flex; justify-content: center;align-items: center;font-size: 36rpx;color: #333;padding: 15rpx 0;.title {margin: 0 15rpx;}.cancel-btn {position: absolute;right: 20rpx;}}.date_week {display: flex;justify-content: center;flex-wrap: wrap;width: 100%;height: 100%;.week_td {width: 101rpx;height: 70rpx;font-size: 30rpx;display: flex;flex-direction: column;justify-content: center;align-items: center;&.select{background: #007aff;.num {color: #fff!important;}}.num {width: 60rpx;height: 60rpx;border-radius: 50%;line-height: 60rpx;text-align: center;&.disabled{color: #ccc;}&.month{color: #333;}&.today{background: #94adff; color:#fff;}}.dot {margin-top: 5rpx;width: 8rpx;height: 8rpx;border-radius: 50%;background: #007aff; }}}}
</style>

这篇关于uni-app实现可选择日期、范围、打点的月份日历的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM