uni-app日期范围选择,颗粒度为`年-月`,支持`至今`选项

2024-01-31 10:18

本文主要是介绍uni-app日期范围选择,颗粒度为`年-月`,支持`至今`选项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

1. 在components中创建sofar-picker.vue

<template><view :class="{'pickerMask':visable}" @click="maskClick" @touchmove.stop.prevent="returnHandle"><view class="picker-box" :class="{'picker-show':visable}"><view class="operate-box" @touchmove.stop.prevent="returnHandle" @tap.stop="returnHandle"><view class="time-box"><view @click="touchSelect(0)" class="time-item" :style="{color:touchIndex?'#000000':themeColor}"><text>{{startText}}</text><text>{{resultDate[0]}}</text></view><text>至</text><view @click="touchSelect(1)" class="time-item" :style="{color:touchIndex?themeColor:'#000000'}"><text>{{endText}}</text><text>{{resultDate[1]}}</text></view></view><view :style="{color:themeColor}" @click="pickerConfirm">确定</view></view><picker-view :value="pickerValue" @change="pickerChange" class="picker-view" :indicator-style="indicatorStyle" @tap.stop="returnHandle"><picker-view-column><view class="picker-item" v-for="(item,index) in yearList" :key="index">{{index>0||touchIndex<1? item+'年' : item+'年' }}</view></picker-view-column><picker-view-column><view class="picker-item" v-for="(item,index) in monthList" :key="index"><text v-show="pickerValue[0]>0||touchIndex<1">{{item}}月</text></view></picker-view-column></picker-view></view></view>
</template><script>export default {name: 'sofarPicker',props: {defaultDate: {type: Array,default: () => []},visable: {type: Boolean,default: false},minYear: {type: Number,default: 1990,},separator: {type: String,default: '.'},themeColor:{type: String,default: '#10BE9D'},startText: {type: String,default: '开始时间'},endText: {type: String,default: '结束时间'}},data() {const date = new Date();const yearList = [];const year = date.getFullYear();const monthList = [];const month = date.getMonth() + 1;for (let i = this.minYear; i <= date.getFullYear(); i++) {yearList.unshift(i);}for (let i = 1; i <= 12; i++) {monthList.push(i);}return {indicatorStyle: 'height: 100rpx;',touchIndex: 0,yearList: yearList,monthList: monthList,year: year,month: month,pickerValue: [0, month - 1],resultDate: []}},mounted() {this.setDate()},methods: {returnHandle(){},maskClick() {this.$emit('update:visable', false)},setDate() {if (this.defaultDate.length > 0) {let date = this.defaultDate[0]this.resultDate = this.defaultDatethis.setPicker(date)} else {let startTime = this.year + this.separator + this.monththis.resultDate = [startTime, '至今']}},setPicker(date) {console.log('setPicker');console.log(date);let startTime = this.year + this.separator + this.monthif (date === '至今') {this.pickerValue = [0, 0]} else {let dateArray = date.split(this.separator)let yearIndex = this.yearList.indexOf(Number(dateArray[0]))let monthIndex = this.monthList.indexOf(Number(dateArray[1]))this.pickerValue = [yearIndex, monthIndex]}	},getDate(date) {let result = ''let year = this.yearList[date[0]]let month = this.monthList[date[1]]if (year === '至今') {result = '至今'} else {result = year + this.separator + month}this.resultDate[this.touchIndex] = result},touchSelect(val) {console.log('touchSelect');let date = this.resultDate[val]if (this.touchIndex === val) {return} else {if (val) {this.yearList.unshift('至今')} else {this.yearList.splice(0, 1)}this.touchIndex = val}if (date) {this.setPicker(date)}},pickerChange(e) {this.pickerValue = e.detail.valuethis.getDate(e.detail.value)},pickerConfirm() {const { resultDate, year, month } = thislet nowTime = new Date(year+'/'+month).getTime()let startTime = new Date(resultDate[0]).getTime()let endTime = resultDate[1] === '至今' ? nowTime : new Date(resultDate[1]).getTime()if (startTime <= endTime && endTime <= nowTime) {this.$emit('confirm', resultDate)this.maskClick()} else {uni.showToast({title: '时间范围错误!',icon: 'none'})}}}}
</script><style lang="scss" scoped>.pickerMask {position: fixed;z-index: 998;top: 0;right: 0;left: 0;bottom: 0;background: rgba(0, 0, 0, 0.6);}.picker-box {position: fixed;bottom: 0;left: 0;width: 100%;transition: all 0.3s ease;transform: translateY(100%);z-index: 998;.operate-box {display: flex;align-items: center;justify-content: space-between;padding: 18rpx 30rpx;background-color: #FFFFFF;text-align: center;font-size: 30rpx;border-bottom: 2rpx solid #e5e5e5;.time-box {width: 60%;display: flex;align-items: center;justify-content: space-between;.time-item {display: flex;flex-direction: column;}}}}.picker-show {transform: translateY(0);}.picker-view {width: 750rpx;height: 600rpx;background-color: #FFFFFF;.picker-item {height: 100rpx;display: flex;align-items: center;justify-content: center;text-align: center;}}
</style>

2. 使用

<template><view class="content"><view class="header"><view class="date-item">年月范围:{{startTime}} - {{endTime}}</view></view><button class="date-btn" type="default" @click="openPicker">打开</button><sofar-picker :visable.sync="pickerVisable" :defaultDate="defaultDate" @confirm="confirm"></sofar-picker></view>
</template><script>import sofarPicker from '@/components/sofar-picker/sofar-picker.vue'export default {components: {sofarPicker},data() {return {pickerVisable: false,defaultDate: [],startTime: '',endTime: ''}},methods: {openPicker() {this.pickerVisable = true},confirm(date) {this.startTime = date[0]this.endTime = date[1]}}}
</script><style>.header {width: 100%;padding: 60rpx 30rpx;display: flex;flex-direction: column;}.date-item {margin-bottom: 30rpx;}.date-btn {width: 500rpx;height: 80rpx;line-height: 80rpx;}
</style>

这篇关于uni-app日期范围选择,颗粒度为`年-月`,支持`至今`选项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

基于Python实现数字限制在指定范围内的五种方式

《基于Python实现数字限制在指定范围内的五种方式》在编程中,数字范围限制是常见需求,无论是游戏开发中的角色属性值、金融计算中的利率调整,还是传感器数据处理中的异常值过滤,都需要将数字控制在合理范围... 目录引言一、基础条件判断法二、数学运算巧解法三、装饰器模式法四、自定义类封装法五、NumPy数组处理

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Java日期类详解(最新推荐)

《Java日期类详解(最新推荐)》早期版本主要使用java.util.Date、java.util.Calendar等类,Java8及以后引入了新的日期和时间API(JSR310),包含在ja... 目录旧的日期时间API新的日期时间 API(Java 8+)获取时间戳时间计算与其他日期时间类型的转换Dur

Python日期和时间完全指南与实战

《Python日期和时间完全指南与实战》在软件开发领域,‌日期时间处理‌是贯穿系统设计全生命周期的重要基础能力,本文将深入解析Python日期时间的‌七大核心模块‌,通过‌企业级代码案例‌揭示最佳实践... 目录一、背景与核心价值二、核心模块详解与实战2.1 datetime模块四剑客2.2 时区处理黄金法