vue 模仿机票自定义日历组件,区间选择
1.创建组件 components > calander > index.vue
<template><div class="page" v-if="show"> <div class="box"> <div class="box_head" v-if="!propshow"><div class="font28" @click="calanderShowData">取消</div><div class="font28" @click="calanderSubmit">确定</div> </div><div class="box-flex"> <div class="flex-item item-content-current-day item-content-tit"> <div class="item-content titleHead">{{currentDate}}</div> </div> </div> </div> <div class="box-flex"> <div class="flex-item"> <div class="item-content">一</div> </div> <div class="flex-item"> <div class="item-content">二</div> </div> <div class="flex-item"> <div class="item-content">三</div> </div> <div class="flex-item"> <div class="item-content">四</div> </div> <div class="flex-item"> <div class="item-content">五</div> </div> <div class="flex-item"> <div class="item-content">六</div> </div> <div class="flex-item"> <div class="item-content">日</div> </div> </div> <div class="box-flex"> <div class="flex-item flexData" v-for='(item, index) in currentDayList' :key='item' @click='itemFun(item.num,mouth[index],index, "first", item.state)' :class="{'hover-date': Section[index] && item.num, 'noBack': !item.state && item.num && !propshow}"> <div class="item-content red-background" v-if='currentDay === item.num && mouth[index].mouth === currentMouth.mouth && currentMouth.year === mouth[index].year' :class="{'hoverColorData' : Section[index] && item.num, 'hover-date':active[index]}"><p>今天</p><p v-if="item.number && propshow">{{item.number}}展位</p> </div> <div class="item-content backgroundData" v-else :class="{'hover-date':active[index]}" ><p>{{item.num}}</p><p v-if="item.number && propshow">{{item.number}}展位</p></div> </div> </div> <div class="box"> <div class="box-flex"> <div class="flex-item item-content-current-day item-content-tit"> <div class="item-content titleHead">{{currentDateTwo}}</div> </div> </div> </div> <div class="box-flex"> <div class="flex-item"> <div class="item-content">一</div> </div> <div class="flex-item"> <div class="item-content">二</div> </div> <div class="flex-item"> <div class="item-content">三</div> </div> <div class="flex-item"> <div class="item-content">四</div> </div> <div class="flex-item"> <div class="item-content">五</div> </div> <div class="flex-item"> <div class="item-content">六</div> </div> <div class="flex-item"> <div class="item-content">日</div> </div> </div> <div class="box-flex"> <div class="flex-item flexData" v-for='(item, index) in currentDayListData' :key='item' @click='itemFun(item.num,mouths[index],index, "last", item.state)' :class="{'hover-date': Sections[index] && item.num, 'noBack': !item.state && item.num && !propshow}"> <div class="item-content backgroundData" :class="{'hover-date':actives[index]}"><p>{{item.num}}</p><p v-if="item.number && propshow">{{item.number}}展位</p></div> </div> </div> <div v-if="propshow" class="propshowData"></div> </div> </template> <script> import vue from 'vue' export default {name: 'calander',data () {return {calanderList: [],show: false,currentDate: '2017年05月03日',currentDateTwo: '2017年06月03日',dayList: '',currentDayList: [],currentObj: '',currentDay: '',mouth: [],currentMouth: null,hoverDate: {'date': null, 'mouth': null, 'year': null},activeClick: 0,active: [],actives: [],Section: [],firstIndex: 0,lastIndex: 0,currentDayListData: [],currentJudge: false,currentObjNew: '',Sections: [],lastType: '',fristType: '',numArray: [],numArrayTwo: []}},props: {propshow: false,boothArray: [], // 从父级传来的数组,中主要携带number展位数,calanderList: [],yearAndmouthFirst: {},yearAndmouthTwo: {},calanderShow: false},mounted () {let timeData = this.getNextMonth(new Date(),1)var currentObj = this.getCurrentDayString()this.currentDate = currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月' + currentObj.getDate() + '日'this.currentDateTwo = timeData.getFullYear() + '年' + (timeData.getMonth() + 1) + '月' + timeData.getDate() + '日'// this.currentObjNew = currentObj.getFullYear() + '/' + (currentObj.getMonth() + 2) + '/' + currentObj.getDate()this.currentDay = currentObj.getDate()this.currentObj = currentObjthis.currentMouth = {mouth: currentObj.getMonth() + 1,year: currentObj.getFullYear()}setTimeout(() => {this.setSchedule(currentObj, 'newMonth') this.setSchedule(this.getNextMonth(new Date(),1), 'nextMonth') // this.setSchedule(new Date(this.currentObjNew), 'nextMonth') },100);},methods: { monthDay: function (year, month) {month = parseInt(month, 10);var d = new Date(year, month, 0); //这个是都可以兼容的var date = new Date(year + "/" + month + "/0") //IE浏览器可以获取天数,谷歌浏览器会返回NaNreturn d.getDate();},getNextMonth: function (date,length) {let yy = date.getFullYear()let mm = date.getMonth()let dd = date.getDate()let nm= 0//目标月份 nm = mm+lengthlet nd = 0//目标天数if(this.monthDay(yy,nm+1)<dd){nd = this.monthDay(yy,nm+1)}else{nd = dd-1}date.setDate(1)date.setMonth(nm)date.setDate(nd)return date},calanderSubmit(){console.log(this.numArray)this.$emit('assignment', this.numArray)},calanderShowData(){this.$emit('calanderShowClick', this.calanderShow)},formatTwo (n) {if (n >= 10) return nelse return '0' + n},// 判断日历的当前日期是否不可点击 isDisabled (calander, index, item) {// if (new Date(calander).getTime() < new Date().getTime()) {// this.mouth[index].state = 'disabled'// return false// } },getWeekDayNextMonth(){var myDate = new Date()myDate.setMonth(myDate.getMonth() + 1);myDate.setDate(1);var weekDay=new Array(7, 1, 2, 3, 4, 5, 6)return weekDay[myDate.getDay()];},itemFun (item, yearAndmouth, index, type, stateClick) { if (stateClick) {let dataD = new Date();let curMonthDays = new Date(dataD.getFullYear(), (dataD.getMonth()+1), 0).getDate()this.hoverDate = {'date': item, 'mouth': yearAndmouth.mouth, 'year': yearAndmouth.year}if (type == 'first') {var activeArrayData = this.activevar SectionArrayData = this.Section} else {var activeArrayData = this.activesvar SectionArrayData = this.Sections }if (this.activeClick == 0) {this.yearAndmouthFirst = yearAndmouththis.yearAndmouthFirst.date = itemfor(let i=0; i<this.active.length; i++){this.active[i] = falsevue.set(this.active, i, this.active[i]) }for(let i=0; i<this.actives.length; i++){this.actives[i] = falsevue.set(this.actives, i, this.actives[i]) }for(let t=0; t<this.Section.length; t++){this.Section[t] = falsevue.set(this.Section, t, this.Section[t])}for(let t=0; t<this.Sections.length; t++){this.Sections[t] = falsevue.set(this.Sections, t, this.Sections[t])}this.numArray = []this.activeClick = 1this.fristType = typethis.firstIndex = itemthis.yearFirst = yearAndmouth.yearthis.mouthFirst = yearAndmouth.mouthactiveArrayData[index] = truevue.set(activeArrayData, index, activeArrayData[index])this.numArray.push(this.yearAndmouthFirst)console.log(this.numArray)} else if (this.activeClick == 1){ this.numArray = []this.yearAndmouthTwo = yearAndmouththis.yearAndmouthTwo.date = itemif (this.firstIndex != index) {this.activeClick = 0this.lastIndex = itemthis.lastType = typethis.yeaTwo = yearAndmouth.yearthis.mouthTwo = yearAndmouth.mouthactiveArrayData[index] = truevue.set(activeArrayData, index, activeArrayData[index])if (this.fristType == this.lastType) {if (this.lastIndex > this.firstIndex) {var indexCenter = this.lastIndex - this.firstIndex + 1 } else {var indexCenter = this.firstIndex - this.lastIndex + 1 }for(let j=0; j<indexCenter; j++){if (this.lastIndex < this.firstIndex) {var indexData = j + indexthis.numArray.push({'date': item + j, 'mouth': yearAndmouth.mouth, 'year': yearAndmouth.year})} else {this.numArray.unshift({'date': item - j, 'mouth': yearAndmouth.mouth, 'year': yearAndmouth.year}) var indexData = index - j}SectionArrayData[indexData] = truevue.set(SectionArrayData, indexData, SectionArrayData[indexData])}} else {if (this.fristType == 'first') {var indexCenter = curMonthDays - this.firstIndex + 1var indexCenters = this.lastIndex} else {var indexCenter = curMonthDays - this.lastIndex + 1var indexCenters = this.firstIndex }for(let j=0; j<indexCenter; j++){if (this.fristType == 'first') {var indexData = this.firstIndex + jthis.numArray.push({'date': indexData, 'mouth': this.mouthFirst, 'year': this.yearFirst}) } else {var indexData = this.lastIndex + jthis.numArray.push({'date': item + j, 'mouth': this.mouthTwo, 'year': this.yeaTwo}) }this.Section[indexData + 1] = truevue.set(this.Section, indexData, this.Section[indexData]) } var monthData = this.getWeekDayNextMonth()for(let j=0; j<indexCenters; j++){if (this.fristType == 'first') {var indexDatas = j + monthData - 1this.numArrayTwo.unshift({'date': item - j, 'mouth': this.mouthTwo, 'year': this.yeaTwo}) } else {var indexDatas = this.firstIndex - j + monthData -2this.numArrayTwo.unshift({'date': this.firstIndex - j , 'mouth': this.mouthFirst, 'year': this.yearFirst}) }this.Sections[indexDatas] = truevue.set(this.Sections, indexDatas, this.Sections[indexDatas])}}this.numArray = this.numArray.concat(this.numArrayTwo)} }}},doDay: function (e) {var that = thisvar currentObj = that.currentObjvar Y = currentObj.getFullYear()var m = currentObj.getMonth() + 1let newData = new Date let newMonth=newData.getMonth()+1;let d = currentObj.getDate()var str = ''if (e.currentTarget.dataset.key === 'left') {m -= 1if (m <= 0) {str = (Y - 1) + '/' + 12 + '/' + d} else {str = Y + '/' + m + '/' + d}this.currentJudge = true} else if(e.currentTarget.dataset.key === 'right') {if (newMonth !== m) {m += 1if (m <= 12) {str = Y + '/' + m + '/' + d} else {str = (Y + 1) + '/' + 1 + '/' + d}this.currentJudge = true} else {this.currentJudge = false }} else if (e.currentTarget.dataset.key === 'leftTwo'){ if (newMonth !== m) {m -= 1if (m <= 0) {str = (Y - 1) + '/' + 12 + '/' + d} else {str = Y + '/' + m + '/' + d}this.currentJudge = true} else {this.currentJudge = false}} else if (e.currentTarget.dataset.key === 'rightTwo'){m += 1if (m <= 12) {str = Y + '/' + m + '/' + d} else {str = (Y + 1) + '/' + 1 + '/' + d}this.currentJudge = true}if (this.currentJudge) {currentObj = new Date(str)this.currentDate = currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月' + currentObj.getDate() + '日'this.currentDateTwo = currentObj.getFullYear() + '年' + (currentObj.getMonth() + 2) + '月' + currentObj.getDate() + '日' this.currentObjNew = currentObj.getFullYear() + '/' + (currentObj.getMonth() + 2) + '/' + currentObj.getDate() + '/' this.currentObj = currentObjif (e.currentTarget.dataset.key == 'left' || e.currentTarget.dataset.key == 'right') {this.setSchedule(currentObj, 'newMonth')} else {this.setSchedule(new Date(this.currentObjNew), 'nextMonth')}}},getCurrentDayString: function () {var objDate = this.currentObjif (objDate !== '') {return objDate} else {var cobj = new Date()var a = cobj.getFullYear() + '/' + (cobj.getMonth() + 1) + '/' + cobj.getDate()return new Date(a)}},setSchedule: function (currentObj, type) {var that = thisvar m = currentObj.getMonth() + 1var Y = currentObj.getFullYear()var d = currentObj.getDate()var newDate = new Date()var newYear = newDate.getFullYear()var newMonth = newDate.getMonth() + 1// var dayString = Y + '/' + m + '/' + currentObj.getDate()var currentDayNum = new Date(Y, m, 0).getDate()var currentDayWeek = currentObj.getUTCDay() + 1var result = currentDayWeek - (d % 7 - 1)var firstKey = result <= 0 ? 7 + result : resultvar currentDayListNew = []var mouth = []var f = 0for (var i = 0; i < 42; i++) {mouth[i] = {'mouth': m,'year': Y}currentDayListNew[i] = {'num': '','number': '','state': false}if (i < firstKey - 1) {currentDayListNew[i].num = ''} else {if (f < currentDayNum) {currentDayListNew[i].num = f + 1f = currentDayListNew[i].numif (newYear == Y && newMonth == m) {if (f >= d) {currentDayListNew[i].state = true}} else {if (f <= d) {currentDayListNew[i].state = true}}} else if (f >= currentDayNum) {currentDayListNew[i].num = ''}if (this.boothArray.length > 0) {this.boothArray.forEach((element, indexArray) => {if (element.year == Y && element.mouth == m) {if (element.date == currentDayListNew[i].num) {currentDayListNew[i].number = String(element.number)}}})}if (this.calanderList.length > 0) {this.calanderList.forEach((element, indexList) => {if (element.year == Y && element.mouth == m) {if (element.date == currentDayListNew[i].num) {if (type == 'newMonth') {this.Section[i] = true} else {this.Sections[i] = true} }}});} }}if (type == 'newMonth') {that.currentDayList = currentDayListNewthat.mouth = mouth} else {that.currentDayListData = currentDayListNew that.mouths = mouth }this.show = true }} } </script><style scoped> .box_head{height: 1.173333rem /* 88/75 */;background: #fff;display: flex;justify-content: space-between;align-items: center;padding: 0 .4rem /* 30/75 */;border-bottom: .013333rem /* 1/75 */ solid #E5E5E5; } .noBack{background: #bbb!important;/* border-radius: 5px!important; */ } .hoverColorData{color: #fff!important; } .propshowData{position: absolute;left: 0;right: 0;bottom: 0;top: 0;background: rgba(0, 0, 0, 0.1);z-index: 9999; } .page{background: #fff;position: relative; } .titleHead{margin: .266667rem /* 20/75 */ 0!important; } .sectionBack{background: #8A2BE2; } page { background-color: #2a8cef; background:-webkit-gradient(linear, 0 0, 0 bottom, from(#2a8cef), to(#8A2BE2)); display: flex; flex-direction: column; width: 100%; flex-wrap: nowrap; justify-content: flex-start; align-items: stretch; font-size: 16px; color: #222; } .disabled{color: #ccc !important; } .hover-date{background: #367EED;color: #fff !important;border-radius: 5px; } .box { display: block; /* margin: 10px; */border-top: .013333rem /* 1/75 */ solid #efefef; } .glyphicon-triangle-left img{transform:rotate(180deg) } .glyphicon img{width:12px;height: 19px;margin-top:15px;} .box-flex { display: -webkit-box; display: -webkit-flex; display: flex; flex-wrap: wrap; padding: 0 .133333rem /* 10/75 */; } .flex-item { flex-flow: nowrap; flex-grow: 1; flex-shrink: 1; width: 14%; margin: 1px 1px 1px 0px; } .item-content { margin: 1px; padding: 5px 1px; text-align: center; /* height: 45px; *//* line-height: 45px; */font-size:14px;position: relative;flex-wrap: wrap;justify-content: center; } .item-content i{width:100%;text-align: center;left:0;position: absolute;top:8px; } .item-content span{width:100%;text-align: center;left:0;position: absolute;top:3px;font-size:12px;color: #367EED;line-height: 1; } .item-content-tit{width:30%;/* margin: .133333rem 10/75 0; */ } .item-content-tit .item-content{font-size:17px; }.red-background{color:#367EED;border-radius: 5px;}.item-content-current-day { flex-grow: 2; } </style>
2.页面中引用组件
<calander @assignment="assignment" :propshow="propshow" :boothArray="boothArray" :calanderList="calanderList"></calander> <script>import calander from '@/components/calander'export default {components: {calander},data() {return {calanderList: [],boothArray: [],propshow: false,};},methods: {assignment(){// 组件调取父级方法 },}} </script>
转载于:https://www.cnblogs.com/gqx-html/p/9962148.html
vue 模仿机票自定义日历组件,区间选择相关推荐
- 微信小程序自定义日历组件
微信小程序自定义日历组件 wxml <view class="maskWrap" bindtap="close"></view>< ...
- VUE自定义日历组件,计算年月日,上个月份的空白展示,点击某一天进入详情页面
效果图: 背景描述: 产品提出需求,需要日历来配置每一天的商品价格.刚开始使用的element-ui的el-calendar组件,由于样式.跳转.点击事件等功能都不好控制,所以自己写了一个日历组件.完 ...
- Vue手写一个日历组件
工作中遇到一个需求是根据日历查看某一天/某一周/某一月的睡眠报告,但是找了好多日历组件都不是很符合需求,只好自己手写一个日历组件,顺便记录一下. 先来看看设计图是什么样式, 跟其他日历有点不一样,这个 ...
- vue移动端项目日历组件,月周切换,点击进入上/下一个月
项目场景: Vue移动端项目的日历组件,移动端如果没有别的特别要求,一般用vant中的日历组件就OK,这里用的另一个.组件是网上找的,原网址:vue-hash-calendar,需要的请自行去看. 我 ...
- android gridview控件使用详解_Android开发实现自定义日历、日期选择控件
点击上方蓝字关注 ?? 来源: wenzhihao123 https://www.jianshu.com/p/a2f102c728ce 前言 最近项目需要日历效果,考虑用第三方的反而不太适合设计需求, ...
- calendar vue日期选择组件 基于 vue 2.0高性能日历组件(vue2-datepick)
一.点击文本框,选择日期,把日期赋值到文本框中. 二.日期组件使用 1.安装vue2-datepick npm install vue2-datepick --save 2.初始化,在main.js中 ...
- vue自定义日历组件
代码地址 https://github.com/nan1010082085/vue-components/tree/master/CustomCalendar 享一下组件目录 tip: 组件功能: 展 ...
- vue 自定义日历组件
<template> <div class=""> <div class="calendarTraffic" name=" ...
- 手机端自定义日历组件(vue)
文章目录 一.HTML 二.JS 三.CSS 4.最终效果 可复制查看效果.该代码为完整代码,同时,里面返回的字段可根据自己的业务需求进行更改. 关键字段:dataList是后台传过来需要显示在日历上 ...
最新文章
- Docker 入门系列(1)- 初识容器,镜像、容器、仓库的区别
- clion register
- 华为防火墙的技术积累
- Redis两种持久化方式(RDBAOF)
- C#操作SQLite数据库时出现“Insufficient parameters supplied to the command”的错误
- 位向量(bit vector)(算法导论第十一章11.1-2)
- angular点击按钮弹出页面_Win10提示“由于启动计算机时出现了页面文件配置问题”解决方法...
- 这 5 条 IntelliJ IDEA 调试技巧太强了!
- 钓鱼(信息学奥赛一本通-T1431)
- 如何用控制台启动一个wcf服务
- Python、TensorFlow、机器学习、深度学习四件套(附免费下载)
- “一切即代码”究竟意味着什么?
- SpringCloud中 Feign结合Hystrix断路器开发。
- 19.高性能MySQL --- 锁的调试
- 使用PHP+LibreOffice实现word转html的功能
- 开源单点登录系统CAS
- Servlet实现登录带有验证码验证案例
- 收藏 | 江苏省各地教师公务员等实际工资爆料
- 十款代码表白特效,一个比一个浪漫
- SiamRPN++理解
热门文章
- prerender ajax,Vue Prerender.io 预渲染seo优化
- 信息论与编码技术之无失真信源编码小结
- android 网络mp3格式,Android-使用MediaPlayer播放网络音频并且缓存
- 潮鞋购物网站,html5+css3+js,实现基本的添加购物车,自动计算金额等功能
- 小米股价下滑或是因为业界担心其遭遇竞争对手压制
- 时钟合成芯片-CDCE913-Linux驱动调试-(1)
- 禁用浏览器的兼容模式
- android新手 ppt展示 画板 AsyncTask异步(room数据库 快速本地查询) 数据保存及动态更新
- 移动端禁止缩放meta标签
- 怎样批量修改cad标注文字?