這篇文章主要介紹微信小程序如何實現(xiàn)自定義日期控件,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
在武陟等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作定制網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,外貿(mào)網(wǎng)站建設(shè),武陟網(wǎng)站建設(shè)費用合理。
最近在研究微信小程序,需要實現(xiàn)一個自定義日期的功能,類似于貓眼app上選擇日期,可以按日、按周、按月、按年、自定義日期,為此特地將其寫成了一個組件,供有需要的朋友參考。
本人所用框架是wepy,所以此組件結(jié)構(gòu)為wepy代碼結(jié)構(gòu)。
大致介紹:
1.按日的日歷控件采用的極點日歷插件,詳情請看:https://github.com/czcaiwj/calendar;
2.組件中所用到的一些組件為ivew weapp組件,詳情使用請看我的另一篇分享:https://www.jianshu.com/p/e07b7cf5e494;
3.組件中按周選擇,周我是從2017年至今取的,具體實現(xiàn)可看我另一篇分享:https://www.jianshu.com/p/5112df795162;在本篇onLoad()方法里也有實現(xiàn);因本人開發(fā)需要,設(shè)定功能為按周可多選,這個可以根據(jù)自身開發(fā)需求調(diào)整;
4.本組件時間默認(rèn)從17年開始至今,也可根據(jù)自身實際需求調(diào)整。
實現(xiàn)效果:
代碼如下:
<style type="less"> .home { height: 100%; .calendar-header { font-size: large; color: #406BF8; } .calendar-board { color: #406BF8; } .week_year_style { font-size: 30 rpx; padding: 20 rpx 15 rpx; text-align: center; } .week_selectyear_style { background-color: #fff; color: #406BF8; } .calendar { background-color: #fcfcfc; //padding-top: 10px; } } </style> <template> <view class="home page"> <i-tabs current="{{ currentTab }}" color="#406BF8" bindchange="handleChangeTab"> <i-tab key="按日" title="按日"></i-tab> <i-tab key="按周" title="按周"></i-tab> <i-tab key="按月" title="按月"></i-tab> <i-tab key="按年" title="按年"></i-tab> <i-tab key="自定義" title="自定義"></i-tab> </i-tabs> <view hidden="{{currentTab != '按日'}}" style="margin-top: 30rpx"> <calendar cell-size="40" show-more-days="true" lunar="true" weeks-type="cn" calendar-style="calendar" header-style="calendar-header" board-style="calendar-board" days-color="{{days_style}}" start-date="2017-01" end-date="2018-08" binddayClick="dayClick" bindnextMonth="next" bindprevMonth="prev" /> </view> <view hidden="{{currentTab != '按周'}}"> <view style="display: flex"> <view style="width: 160rpx;background-color: #f6f6f6;border-right: 1rpx solid #e6e6e6"> <block wx:for="{{yearArray}}" wx:for-item="item" wx:key="item"> <view class="week_year_style {{weeksYears==item?'week_selectyear_style':''}}" bindtap='changeWeekYear({{item}})'>{{item + '年'}} </view> </block> </view> <scroll-view scroll-y style="height: 600px;"> <i-cell-group> <block wx:for="{{weeksArray[weeksYears]}}" wx:for-item="category" wx:key="category"> <i-cell title="{{category.weeks}}" bindtap="handleWeeksChange('{{index}}')"> <i-icon wx:if="{{category.select}}" type="right" size="20" color="#406BF8" slot="footer"/> </i-cell> </block> </i-cell-group> </scroll-view> </view> </view> <view hidden="{{currentTab != '按月'}}"> <view style="display: flex"> <view style="width: 160rpx;background-color: #f6f6f6;border-right: 1rpx solid #e6e6e6"> <block wx:for="{{yearArray}}" wx:for-item="item" wx:key="item"> <view class="week_year_style {{monthYear==item?'week_selectyear_style':''}}" bindtap='changeMonthYear({{item}})'>{{item + '年'}} </view> </block> </view> <scroll-view scroll-y style="height: 600px;"> <i-cell-group> <block wx:for="{{monthYear==year?currentMonthArray:fullMonthArray}}" wx:for-item="category" wx:key="*this"> <i-cell title="{{category+'月'}}" bindtap="handleMonthChange('{{category}}')"> <i-icon wx:if="{{category==selectMonth}}" type="right" size="20" color="#406BF8" slot="footer"/> </i-cell> </block> </i-cell-group> </scroll-view> </view> </view> <view hidden="{{currentTab != '按年'}}"> <i-cell-group> <block wx:for="{{yearArray}}" wx:for-item="category" wx:key="category"> <i-cell title="{{category+'年'}}" bindtap="handleYearChange('{{category}}')"> <i-icon wx:if="{{category==selectYear }}" type="right" size="20" color="#406BF8" slot="footer"/> </i-cell> </block> </i-cell-group> </view> <view hidden="{{currentTab != '自定義'}}" style="margin-top: 30rpx"> <calendar cell-size="40" show-more-days="true" lunar="true" weeks-type="cn" calendar-style="calendar" header-style="calendar-header" board-style="calendar-board" days-color="{{define_days_style}}" start-date="2017-01" end-date="2018-08" binddayClick="dayClick_define" bindnextMonth="next_define" bindprevMonth="prev_define" /> </view> </view> </template> <script> import wepy from 'wepy'; const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.']; export default class TabsContent extends wepy.component { props = { openDateModal: { type: Boolean, default: false, twoWay: true }, dateSelect: { type: String, default: '', twoWay: true }, dateFilter: { type: String, default: '', twoWay: true }, filterWeekArray: { type: String, default: '', twoWay: true }, filterWeekSelect: { type: String, default: '', twoWay: true } }; data = { currentTab: '按日', //Tabs頁切換 year: new Date().getFullYear(), // 年份 month: new Date().getMonth() + 1, // 月份 day: new Date().getDate(), str: MONTHS[new Date().getMonth()], // 月份字符串 days_style: [{month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986'}], //按日選中日期樣式 selectDate: [], //按日選擇的日期 yearArray: [], //從2017年起至今 weeksYears: new Date().getFullYear(), //按周選擇了哪一年 weeksArray: {}, //從2017年起至今的所有周 selectWeekArray: [], //選擇的周 fullMonthArray: [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], currentMonthArray: [], //今年過了幾個月 monthYear: new Date().getFullYear(), //按月選擇了哪一年 selectMonth: 0, //按月選擇了哪一月 selectYear: 0, //按年選擇了哪一年 define_days_style: [{month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986'}], //自定義日期格式 selectDate_define: [] }; methods = { handleChangeTab({detail}) { this.currentTab = detail.key this.dateFilter = this.currentTab }, dayClick: function (event) { let style = (event.detail.year == this.year && event.detail.month == this.month) ? [{month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986'}, {month: 'current', day: event.detail.day, color: '#fff', background: '#406BF8'}] : [{month: 'current', day: event.detail.day, color: '#fff', background: '#406BF8'}] this.days_style = style this.selectDate = [event.detail.year, event.detail.month, event.detail.day] this.openDateModal = false this.dateFilter = this.currentTab this.dateSelect = [event.detail.year, event.detail.month, event.detail.day] }, next: function (event) { if (event.detail.currentYear == this.year && event.detail.currentMonth == this.month) this.days_style = [{ month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986' }] else this.days_style = [vent.detail.year, event.detail.month, event.detail.day] }, prev: function (event) { this.days_style = [] }, changeWeekYear(year) { this.weeksYears = year }, handleWeeksChange(weekIndex) { this.weeksArray[this.weeksYears][weekIndex].select = !this.weeksArray[this.weeksYears][weekIndex].select if (this.selectWeekArray[this.weeksYears]) { let bool = this.selectWeekArray[this.weeksYears].some(index => index == weekIndex) if (bool) { //真 已存在 delete //if(this.selectWeekArray[this.weeksYears].length>1) this.selectWeekArray[this.weeksYears].remove(weekIndex) } else { //假 push this.selectWeekArray[this.weeksYears].push(weekIndex) this.selectWeekArray[this.weeksYears].sort() } } else { let temp = {} temp[this.weeksYears] = [] let a = [weekIndex] temp[this.weeksYears] = a this.selectWeekArray = temp } this.filterWeekSelect = this.selectWeekArray //回調(diào)到父級頁面 }, changeMonthYear(year) { this.monthYear = year }, handleMonthChange(month) { this.selectMonth = month this.openDateModal = false this.dateFilter = this.currentTab this.dateSelect = [this.monthYear, this.selectMonth] }, handleYearChange(year) { this.selectYear = year this.openDateModal = false this.dateFilter = this.currentTab this.dateSelect = [year] }, dayClick_define: function (event) { let style = [] if (this.selectDate_define.length == 0) { //第一次點擊 this.selectDate_define.push([event.detail.year, event.detail.month, event.detail.day]) if (event.detail.year == this.year && event.detail.month == this.mounth) { style = [{month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986'}, {month: 'current', day: event.detail.day, color: '#fff', background: '#406BF8'}] } this.define_days_style = style } else if (this.selectDate_define.length == 1) { //選中第二個日期就關(guān)閉頁面 this.selectDate_define.push([event.detail.year, event.detail.month, event.detail.day]) this.define_days_style.push({ month: 'current', day: event.detail.day, color: '#fff', background: '#406BF8' }) this.openDateModal = false this.dateFilter = this.currentTab this.dateSelect = this.selectDate_define } else { //重新自定義日期 this.selectDate_define = [[event.detail.year, event.detail.month, event.detail.day]] style = [{month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986'}, {month: 'current', day: event.detail.day, color: '#fff', background: '#406BF8'}] this.define_days_style = style } }, next_define: function (event) { if (event.detail.currentYear == this.year && event.detail.currentMonth == this.month) this.define_days_style = [{ month: 'current', day: new Date().getDate(), color: '#fff', background: '#eb4986' }] else this.define_days_style = [event.detail.year, event.detail.month, event.detail.day] }, prev_define: function (event) { this.define_days_style = [] }, }; onLoad() { let currentYear = new Date().getFullYear() let currentMonth = new Date().getMonth() + 1 for (var i = currentYear; i >= 2017; i--) { this.yearArray.push(i) } for (var i = currentMonth; i >= 1; i--) { this.currentMonthArray.push(i) } let weeksArray = {} //計算2017年至今的所有周 for (var i = 0; i <= this.yearArray.length; i++) { let index = 1; let temp = [] if (i == 0) { //今年 for (let i of createWeeks(this.yearArray[i])) { let start = i[0], end = i[1]; if (end <= new Date().getTime() || (start <= new Date().getTime() && end >= new Date().getTime())) { let a = start <= new Date().getTime() && end >= new Date().getTime() ? '第' + formatDig(index++) + '周(' + formatDate(start) + '日-' + formatDate(end) + '日)' + ' 本周' : '第' + formatDig(index++) + '周(' + formatDate(start) + '日-' + formatDate(end) + '日)' temp.push({'weeks': a, 'select': false}) } } } else { for (let i of createWeeks(this.yearArray[i])) { let start = i[0], end = i[1]; let a = '第' + formatDig(index++) + '周(' + formatDate(start) + '日-' + formatDate(end) + '日)' temp.push({'weeks': a, 'select': false}) } } if (this.yearArray[i] != undefined) { weeksArray[this.yearArray[i]] = temp.reverse(); } } this.weeksArray = weeksArray this.filterWeekArray = weeksArray } } function formatDig(num) { //return num>9?''+num:'0'+num; return num; } function formatDate(mill) { var y = new Date(mill); let raws = [ //y.getFullYear(), formatDig(y.getMonth() + 1), formatDig(y.getDate()), //y.getDay()||7 //獲取星期幾 ]; //let format=['年','月','日 星期']; let format = ['月', '日']; return String.raw({raw: raws}, ...format); } function* createWeeks(year) { const ONE_DAY = 24 * 3600 * 1000; let start = new Date(year, 0, 1), end = new Date(year, 11, 31); let firstDay = start.getDay() || 7, lastDay = end.getDay() || 7; let startTime = +start, endTime = startTime + (7 - firstDay) * ONE_DAY, _endTime = end - (7 - lastDay) * ONE_DAY; yield [startTime, endTime]; startTime = endTime + ONE_DAY; endTime = endTime + 7 * ONE_DAY; while (endTime < _endTime) { yield [startTime, endTime]; startTime = endTime + ONE_DAY; endTime = endTime + 7 * ONE_DAY; } yield [startTime, +end]; } Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; </script>
以上是“微信小程序如何實現(xiàn)自定義日期控件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁標(biāo)題:微信小程序如何實現(xiàn)自定義日期控件
當(dāng)前地址:http://jinyejixie.com/article32/gpgjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、企業(yè)網(wǎng)站制作、做網(wǎng)站、靜態(tài)網(wǎng)站、營銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)