H5移动端月历区间选择开始月份和结束月份

2023-11-22 11:20

本文主要是介绍H5移动端月历区间选择开始月份和结束月份,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在做h5移动端app时,因业务需要,要做按日区间选择开始和结束日期,按月区间选择开始和结束月份,ui组件中有按日区间选择的,没有按月区间选择,所以结合业务自己写了一个。

<template><van-action-sheetv-model="showDate"position="bottom"@close="closeCalendar"title="选择月份"round:style="{ height: '80%' }"ref="monthDate"><ul class="content"><li v-for="(item, index) in years" :key="index" v-scroll><div class="monthTitle">{{ item.year }}年</div><van-row><van-colspan="6"v-for="(childItem, key) in item.children":key="key":class="childItem.monthClass"><divclass="monthList"@click="selectList(item, childItem)":class="childItem.isSelect === true ? 'isSelect' : ''"><div :class="childItem.disabledMonth == true ? 'isDisabled' : ''">{{ childItem.month }}月</div><divclass="startEnd":class="childItem.text == '开始' || childItem.text == '结束'? 'fontSize': ''">{{ childItem.text }}</div></div></van-col></van-row></li></ul><div class="footBtn" default="footer"><div class="footBox"><van-button @click="onReset" type="primary">重 置</van-button><van-button @click="onConfirm" type="primary">确 定</van-button></div></div></van-action-sheet>
</template><script>
import { Toast } from "vant";
export default {props: {isAllowSameDay: {type: Boolean,},calendarshow: {type: Boolean,},yearLength: {type: Number,},defaultDate: {type: Array,required: true,},},data () {return {years: [],monthList: ["一","二","三","四","五","六","七","八","九","十","十一","十二",],showDate: this.calendarshow,selectArr: [],currentYear: new Date().getFullYear(), //当前年份currentMonth: new Date().getMonth() + 1, //当前月份startDefaultYear: new Date().getFullYear(), //开始年份startDefaultMonth: new Date().getMonth() + 1, //开始月份endDefaultYear: new Date().getFullYear(), //结束年份endDefaultMonth: new Date().getMonth() + 1, //结束月份startText: "开始",endText: "结束",startEndText: "开始/结束"};},directives: {// 自定义指令 月历定位到当前年份scroll: {inserted (el) {el.scrollIntoView();},},},watch: {calendarshow (val) {this.showDate = val; //显示隐藏月历},defaultDate (val) {this.startDefaultYear = val[0].getFullYear(); //开始年份this.startDefaultMonth = val[0].getMonth() + 1; //开始月份this.endDefaultYear = val[1].getFullYear(); //结束年份this.endDefaultMonth = val[1].getMonth() + 1; //结束月份this.setDefaultMonth();},},created () {this.initCalendar()},methods: {// 初始化月历initCalendar () {const that = this;for (let i = 1; i <= that.yearLength; i++) {const item = {year: that.currentYear - (that.yearLength - i),children: [],};for (let j = 0; j < that.monthList.length; j++) {let list = {month: that.monthList[j],isSelect: false,value: j + 1,text: "",};// 判断当前月之后的月份不可选if ((item.year == that.currentYear && j + 1 > that.currentMonth) || (item.year == that.currentYear - (that.yearLength - 1) && j + 1 < that.currentMonth)) {list.disabledMonth = true;} else {list.disabledMonth = false;}item.children.push(list);}that.years.push(item);}},// 设置默认值setDefaultMonth () {const that = this;const startYear = that.startDefaultYear; //默认开始年份const endYear = that.endDefaultYear; //默认结束年份const startMonth = that.startDefaultMonth;  //默认开始的年份const endMonth = that.endDefaultMonth;  //默认开始的年份that.selectArr = [];that.years.map((item) => {item.children.map((list) => {list.text = "";list.isSelect = false;// 当前起始年份、月份都相同if (startYear == endYear && startMonth == endMonth) {list.monthClass = "";if (item.year == startYear && list.value == startMonth) {list.text = that.startEndText;list.isSelect = true;that.monthSaveArr(startYear, startMonth, list.text);}} else {// 起始年份相同、月份不同if (item.year == startYear && list.value == startMonth) {list.text = that.startText;list.isSelect = true;that.monthSaveArr(item.year, list.value, list.text);}if (item.year == endYear && list.value == endMonth) {list.text = that.endText;list.isSelect = true;that.monthSaveArr(item.year, list.value, list.text);}}});});},// 选择的月份存入一个数组中monthSaveArr (year, month, text) {const dataItem = {year: year,month: month,text: text};this.selectArr.push(dataItem);},// 选择月份selectList (item, childItem) {const that = this;const selectStartYear = that.selectArr[0].year; //选择开始的年份const selectStartMonth = that.selectArr[0].month;//选择开始的月份// 大于当前月份不可选if (childItem.disabledMonth == true) {return;}// 开始月份  (如果数组为空 或者 已经选了开始结束月份为同一月) 选择开始月份if (that.selectArr.length == 0 || that.selectArr[0].text == that.startEndText) {that.cancelSelectMonth();childItem.text = that.startText;that.monthSaveArr(item.year, childItem.value, childItem.text);} else if (that.selectArr.length == 1) {if (item.year == selectStartYear && childItem.value == selectStartMonth) {// 开始、结束月份是否可选同一天if (that.isAllowSameDay == true) {childItem.isSelect = true;childItem.text = that.startEndText;that.monthSaveArr(item.year, childItem.value, childItem.text);return;} else {childItem.isSelect = true;childItem.text = that.startText;return;}}// 选择的月份不能超过12个月if ((item.year > this.selectArr[0].year && childItem.value >= this.selectArr[0].month) || (item.year - this.selectArr[0].year > 1)) {Toast("结束月份不能超过12个月");return;}if ((item.year == selectStartYear && childItem.value < selectStartMonth) || item.year < selectStartYear) {that.cancelSelectMonth();childItem.text = that.startText;that.monthSaveArr(item.year, childItem.value, childItem.text);} else {// 结束月份childItem.text = that.endText;that.monthSaveArr(item.year, childItem.value, childItem.text);that.setAreaBackground();}} else if (that.selectArr.length == 2) {that.cancelSelectMonth();childItem.text = that.startText;that.monthSaveArr(item.year, childItem.value, childItem.text);}childItem.isSelect = !childItem.isSelect;},// 取消已选中的月份cancelSelectMonth () {this.selectArr = [];this.years.map((item) => {item.children.map((ev) => {ev.text = "";ev.isSelect = false;ev.monthClass = "";});});},// 添加开始、结束区间的背景颜色setAreaBackground () {const startYear = this.selectArr[0].year; //开始年份const endYear = this.selectArr[1].year;   //结束年份const startMonth = this.selectArr[0].month; //开始年份const endMonth = this.selectArr[1].month;   //结束年份this.years.map((item) => {// 起始年份相同if (item.year == startYear && item.year == endYear) {item.children.map((ev) => {if (endMonth - startMonth >= 1) {if (ev.value == startMonth) {ev.monthClass = "calendarStartColor";} else if (ev.value == endMonth) {ev.monthClass = "calendarEndColor";} else if (ev.value > startMonth && ev.value < endMonth) {ev.monthClass = "calendarMiddleColor";}}})} else if (endYear > startYear) {// 开始年份小于结束年份if (item.year == startYear) {// 起始月份item.children.map((ev) => {if (ev.value == startMonth) {ev.monthClass = "calendarStartColor";} else if (ev.value > startMonth) {ev.monthClass = "calendarMiddleColor";}})} else if (item.year == endYear) {// 结束月份item.children.map((ev) => {if (ev.value == endMonth) {ev.monthClass = "calendarEndColor";} else if (ev.value < endMonth) {ev.monthClass = "calendarMiddleColor";}})} else if (item.year > startYear && item.year < endYear) {// 开始月份与结束月份区间的加背景色item.children.map((ev) => {ev.monthClass = "calendarMiddleColor";})}}});},// 确认onConfirm () {this.$emit("confirmMonth", this.selectArr);},// 重置日期onReset () {this.$emit("resetCalendar");},// 关闭closeCalendar () {this.$emit("closeCalendar");}}
};
</script><style lang="stylus" scoped>
/deep/.van-action-sheet__close {right: auto;left: 0px;
}
/deep/.van-action-sheet__header{font-size: 1rem;font-family: PingFang SC;font-weight: bold;color: #333333;
}
.content {font-family: PingFang SC;padding-bottom: 84px;/deep/.van-row {padding: 1.11rem 0 0 0;}/deep/.van-col--6 {margin-bottom: 1.11rem;position: relative;}.calendarStartColor{background-image: linear-gradient(to right, #fff , #e6f5fc);}.calendarEndColor{background-image: linear-gradient(to left,#fff , #e6f5fc);}.calendarMiddleColor {background-color: #e6f5fc;}.monthTitle {background: #E5E5E5;font-size: 0.83rem;font-weight: bold;color: #333333;padding: 0.61rem 0;}.monthList {width: 3rem;height: 2.22rem;display: flex;flex-flow: column;color: #333333;justify-items: center;align-items: center;border-radius: 0.56rem;margin: 0 auto;padding: 0 0.22rem 0.22rem 0.22rem;div:first-child {height: 0.81rem;line-height: 0.81rem;padding: 0.44rem 0 0.22rem 0;font-size: 0.89rem;font-weight: 500;}.startEnd {height: 0.61rem;line-height: 0.61rem;font-size: 0.53rem;font-weight: 500;color: #FFFEFE;}.fontSize {font-size: 0.61rem;}div {display: flex;flex-flow: row;}}.isDisabled {color: #ccc;}.isSelect {background: #0097E0;color: #fff;}
}.footBtn {position: fixed;bottom: 0;width: 100%;padding: 0.66rem 0 1.53rem 0;display: flex;background: #fff;.footBox {padding: 0 16px;display: flex;flex-flow: row;width: 100%;}.van-button {border-radius: 2rem;height: 2.5rem;line-height: 2.5rem;flex: 1;margin: 0 1rem;font-size: 1rem;}.van-button:nth-child(1) {width: 80%;color: #000;font-size: 14px;background: white;border: 2px solid #666 !important;border-radius: 40px;margin: 0 1rem;}.van-button:nth-child(2) {width: 80%;color: #fff;font-size: 14px;background: white;border-radius: 40px;margin: 0 1rem;border: none;background: -webkit-gradient(linear, right top, left top, from(#0068b7), to(#0097e0));background: linear-gradient(-90deg, #0068b7, #0097e0);}
}
</style>

组件引用

<month-filter :defaultDate="monthDate.monthDefaultDate" :calendarshow="monthDate.monthCalendarShow" :isAllowSameDay="monthDate.isAllowSameDay" :yearLength="monthDate.yearLength" @closeCalendar="monthDate.monthCalendarShow = false" @confirmMonth="selectMonth" @resetCalendar="resetMonthCalendar">
</month-filter>monthDate:{monthCalendarShow:false,yearLength:10,monthDefaultDate:[new Date(), new Date() //设置区间  ],isAllowSameDay:true     // 可选同一个月份
},// 选择月份区间
selectMonth(data) {if(data.length ==2 ){// 这里是获取的开始和结束的值}else if(data.length==1){if(data[0].text !="开始/结束"){Toast("请选择正确的月份区间");return;}else{// 这里是开始和结束的月份相同}}this.monthDate.monthCalendarShow = false;
},resetMonthCalendar(){ //重置月份}

 按月区间选择组件还可优化,欢迎大家给出意见,谢谢!

效果图如下:

开始月份到结束月份

 开始结束月份可选当月:

 

这篇关于H5移动端月历区间选择开始月份和结束月份的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

如何选择适合孤独症兄妹的学校?

在探索适合孤独症儿童教育的道路上,每一位家长都面临着前所未有的挑战与抉择。当这份责任落在拥有孤独症兄妹的家庭肩上时,选择一所能够同时满足两个孩子特殊需求的学校,更显得尤为关键。本文将探讨如何为这样的家庭做出明智的选择,并介绍星贝育园自闭症儿童寄宿制学校作为一个值得考虑的选项。 理解孤独症儿童的独特性 孤独症,这一复杂的神经发育障碍,影响着儿童的社交互动、沟通能力以及行为模式。对于拥有孤独症兄

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

我在移动打工的日志

客户:给我搞一下录音 我:不会。不在服务范围。 客户:是不想吧 我:笑嘻嘻(气笑) 客户:小姑娘明明会,却欺负老人 我:笑嘻嘻 客户:那我交话费 我:手机号 客户:给我搞录音 我:不会。不懂。没搞过。 客户:那我交话费 我:手机号。这是电信的啊!!我这是中国移动!! 客户:我不管,我要充话费,充话费是你们的 我:可是这是移动!!中国移动!! 客户:我这是手机号 我:那又如何,这是移动!你是电信!!

hdu4267区间统计

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import

hdu4417区间统计

给你一个数列{An},然后有m次查询,每次查询一段区间 [l,r] <= h 的值的个数。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamRead

hdu3333区间统计

题目大意:求一个区间内不重复数字的和,例如1 1 1 3,区间[1,4]的和为4。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;

如何选择SDR无线图传方案

在开源软件定义无线电(SDR)领域,有几个项目提供了无线图传的解决方案。以下是一些开源SDR无线图传方案: 1. **OpenHD**:这是一个远程高清数字图像传输的开源解决方案,它使用SDR技术来实现高清视频的无线传输。OpenHD项目提供了一个完整的工具链,包括发射器和接收器的硬件设计以及相应的软件。 2. **USRP(Universal Software Radio Periphera