taro ui 小程序at-calendar日历组件自定义样式+选择范围日历崩溃处理

本文主要是介绍taro ui 小程序at-calendar日历组件自定义样式+选择范围日历崩溃处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

taro ui 日历文档

目录

单选+标记时间:

效果:

template:

data:

methods:

日历--范围选择:

效果:

template:

data:

methods:

日历--间隔多选:利用标记日期实现不连续多选日期

效果:

template:

data:

mothods:

 css:



单选+标记时间:

效果:

template:

<at-calendar class="zs-calendar" :marks="marks" @monthChange="monthChange" @dayClick="selectDate" />

data:

const currentDate = ref(dateFormat(Date.now(), 'YYYY-MM-DD'))
const marks = ref([]) // 日历打点 [{ value: '2024-06-10' }, { value: '2024-06-12' }]// 日历前面会展示上个月的几个日期,在数据查询的时候,可以查询从上个月20号开始到下个月20号,确保展示出来的日期都有数据
const startDate = ref(getSpecialDate(currentDate.value, 1, 0, 20))
const endDate = ref(getSpecialDate(currentDate.value, 0, 1, 20))

methods:

// 日历上点击某个日期
const selectDate = (data) => {currentDate.value = data.value
}// 切换月份时 更新开始日期 结束日期
const monthChange = (date) => {startDate.value = getSpecialDate(date, 1, 0, 20)endDate.value = getSpecialDate(date, 0, 1, 20)
}// 获取前n个月or下n个月的某天
// date 参照日期 2024-06-17,lastMonth前n个月 || nextMonth下n个月,day 20号
// getLastMonthTwentieth('2024-06-17', 1, 0, 20) 获取上个月20号
// getLastMonthTwentieth('2024-06-17', 0, 1, 6) 获取下个月6号
export const getSpecialDate = (date, lastMonth, nextMonth, day) => {const now = new Date(date); // 参照日期const specialDate = nextMonth == 0 ? new Date(now.getFullYear(), now.getMonth() - lastMonth, day) : new Date(now.getFullYear(), now.getMonth() + nextMonth, day);return dateFormat(specialDate, 'YYYY-MM-DD');
}/*** 将时间戳转换为时间日期(如:2021-07-12 10:20:35)*/
export const dateFormat  = (timestamp, format) => {if (String(timestamp).length === 10) {timestamp = timestamp * 1000}let date = new Date(timestamp)let Y = date.getFullYear()let M = date.getMonth() + 1let D = date.getDate()let hour = date.getHours()let min = date.getMinutes()let sec = date.getSeconds()if (format === 'YYYY') {return Y // 2021} else if (format === 'YYYY-MM') { // 2021-07return Y + '-' + (M < 10 ? '0' + M : M)} else if (format === 'YYYY-MM-DD') { // 2021-07-12return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D)} else if (format === 'HH:mm:ss') { // 10:20:35return (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)} else if (format === 'YYYY-MM-DD HH:mm') { // 2021-07-12 10:20return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min)} else if (format === 'YYYY-MM-DD HH:mm:ss') { // 2021-07-12 10:20:35return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)} else {return '--'}
}

日历--范围选择:

效果:

template:

<at-calendar class="zs-calendar" :min-date="minDate" :marks="marks" :isMultiSelect="true" :currentDate="multiCurrentDate" @monthChange="monthChange" @selectDate="onSelectDate" /> 

data:

// 多选日历 日期
let multiCurrentDate = ref({ start: dateFormat(Date.now(), 'YYYY-MM-DD') })
// 多选 最小能选择的日期 明天
const minDate = ref(dateFormat(new Date(new Date().getTime() + 24 * 60 * 60 * 1000), 'YYYY-MM-DD'))
// 日历打点 [{ value: '2024-06-10' }, { value: '2024-06-12' }]
const marks = ref()

methods:

// 多选日历 方法
const onSelectDate = (selectedDates) => {// 这块很重要!!不写会崩溃// 点击的开始日期 = 上次的开始日期 or 点击的开始日期 = 上次的结束日期 ==》 重置选择范围if (multiCurrentDate.value.start == selectedDates.value.start || multiCurrentDate.value.end == selectedDates.value.start) {multiCurrentDate.value = { start: selectedDates.value.start }}// 点击的日期 有开始和结束if (selectedDates.value.end) {multiCurrentDate.value = selectedDates.value}// 无结束日期 =》重置dataList 重置if (!selectedDates.value.end) { }
}

日历--间隔多选:利用标记日期实现不连续多选日期

效果:

template:

<at-calendar class="zs-calendar zs-calendar-multi" :currentDate="currentDateMulti" :min-date="minDate" :marks="selectDataList" @dayClick="selectDateMulti" />

data:

// 多选 日历 当前日期
const currentDateMulti = ref(dateFormat(Date.now(), 'YYYY-MM-DD'))
// 多选 不连续 多选 选择的日期 [{ value: '2024-06-10' }, { value: '2024-06-12' }]
const selectDataList = ref([])
// 多选 最小能选择的日期 明天
const minDate = ref(dateFormat(new Date(new Date().getTime() + 24 * 60 * 60 * 1000), 'YYYY-MM-DD'))

mothods:

const selectDateMulti = (data) => {// 先赋值,防止dom不变currentDateMulti.value = data.valueconst list = JSON.parse(JSON.stringify(selectDataList.value))const index = list.findIndex(item => { return item.value == data.value })// 存在且就剩这2个if (list.length == 2 && index > -1) {Taro.showToast({title: '请至少选择2个日期',icon: 'none',duration: 2000})}// 不存在if (index == -1) {list.push({ value: data.value })dataList.value.push(data.value)setTimeout(() => {currentDateMulti.value = data.value}, 10)}// 存在 且剩多个if (list.length > 2 && index > -1) {list.splice(index, 1)dataList.value.splice(index, 1)setTimeout(() => {currentDateMulti.value = list[0].valuecurrentDateMulti.value = list[list.length - 1].value}, 10)}selectDataList.value = JSON.parse(JSON.stringify(list))}

 css:

// @import "./zs-style.scss";
$zs-color-primary:#4264E2;.zs-calendar .at-calendar__controller {justify-content: space-between;padding-left: 32px;padding-right: 32px;
}
.zs-calendar .at-calendar__list.flex {color: #333333;
}
.zs-calendar .at-calendar__list.flex .flex__item-extra .extra-marks .mark {background-color: $zs-color-primary;color: $zs-color-primary;
}
.zs-calendar .at-calendar__list.flex .flex__item--selected .extra-marks .mark {background-color: white;color: white;
}
.zs-calendar .at-calendar__list.flex .flex__item--selected-head.flex__item--selected-tail .flex__item-container {background-color: $zs-color-primary;
}
.zs-calendar .at-calendar__list.flex .flex__item--today {color: $zs-color-primary;
}
.zs-calendar .at-calendar__list.flex .flex__item--selected {color: white;background-color: $zs-color-primary;
}
.zs-calendar .at-calendar__list.flex .flex__item--selected-head.flex__item--selected-tail {background-color: transparent;
}
.zs-calendar .at-calendar__list.flex .flex__item:nth-child(7n).flex__item--now:not(.flex__item--selected) {color: #aaaaaa;
}
.zs-calendar .at-calendar__list.flex .flex__item:nth-child(7n+1).flex__item--now:not(.flex__item--selected) {color: #aaaaaa;
}// 多选样式 -- 浅蓝色背景圆点
.zs-calendar-multi .at-calendar__list.flex .flex__item-extra .extra-marks {bottom: 3px;z-index: -1;
}
.zs-calendar-multi .at-calendar__list.flex .flex__item-extra .extra-marks .mark {background-color: #eef7ff;// color: #eef7ff;color: transparent;// color: azure;width: 70px;height: 70px;
}

这篇关于taro ui 小程序at-calendar日历组件自定义样式+选择范围日历崩溃处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

java程序远程debug原理与配置全过程

《java程序远程debug原理与配置全过程》文章介绍了Java远程调试的JPDA体系,包含JVMTI监控JVM、JDWP传输调试命令、JDI提供调试接口,通过-Xdebug、-Xrunjdwp参数配... 目录背景组成模块间联系IBM对三个模块的详细介绍编程使用总结背景日常工作中,每个程序员都会遇到bu

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python异常处理之避免try-except滥用的3个核心原则

《Python异常处理之避免try-except滥用的3个核心原则》在Python开发中,异常处理是保证程序健壮性的关键机制,本文结合真实案例与Python核心机制,提炼出避免异常滥用的三大原则,有需... 目录一、精准打击:只捕获可预见的异常类型1.1 通用异常捕获的陷阱1.2 精准捕获的实践方案1.3

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别