[前端] 微信小程序日期选择器使用详解

1705 0
王子 2022-10-21 15:23:38 | 显示全部楼层 |阅读模式
本文实例为大家分享了微信小程序日期选择器的具体代码,供大家参考,具体内容如下
需求:在小程序开发中,时常会遇到日期选择器、时间选择器或者地区选择器来进行选择的功能。往往设计图上面并不是按部就班沿用官方提供那种控件样式来实现显示,比如:样式会多样化、功能会复杂化。这时我们就要自己写一个适合需求的组件。
下面跟大家分享下我写的一个自定义日期选择器组件
首先上效果图看看:


主要步骤:
第一步:首先自定义选择器组件需要用到picker-view跟picker-view-column。使用方法如下~
  1. <picker-view indicator-class="picker-indicator" value="{{pickerIndexList}}" bindchange="bindChangeDate">
  2. <picker-view-column>
  3.   <view wx:for="{{yearList}}" wx:key="index" class="{{pickerIndexList[0]==index?'txt-active':''}}">{{item}}年</view>
  4. </picker-view-column>
  5. <picker-view-column>
  6.   <view wx:for="{{monthList}}" wx:key="index" class="{{pickerIndexList[1]==index?'txt-active':''}}">{{item}}月</view>
  7. </picker-view-column>
  8. <picker-view-column>
  9.   <view wx:for="{{dayList}}" wx:key="index" class="{{pickerIndexList[2]==index?'txt-active':''}}">{{item}}日</view>
  10. </picker-view-column>
  11. </picker-view>
复制代码
第二步:打开选择器时就要获取到当前的年月日,我这里使用了for遍历直接生成年份数组跟月份数组。注:天数根据年份跟月份动态生成
  1. //獲取當前日期
  2. getCurrentDate: function (e) {
  3.   var that = this;
  4.   var yearList = [], monthList = [], dayList = [];
  5.   for (var i = new Date().getFullYear(); i <= 2050; i++) {//年份
  6.    yearList.push(i);
  7.   }
  8. for (var i = 1; i <= 12; i++) {//月份
  9.    monthList.push(i);
  10.   }
  11.   var myDate = new Date();
  12.   var currentYearIndex = yearList.findIndex(o => o == myDate.getFullYear());
  13.   var currentMonthIndex = monthList.findIndex(o => o == myDate.getMonth() + 1);
  14.   var dayList = that.getDayList(currentYearIndex, currentMonthIndex);//天
  15.   var currentDayIndex = dayList.findIndex(o => o == myDate.getDate());
  16.   var pickerIndexList = that.data.pickerIndexList;
  17.   pickerIndexList[0] = currentYearIndex;
  18.   pickerIndexList[1] = currentMonthIndex;
  19.   pickerIndexList[2] = currentDayIndex;
  20.   app.globalData.dateIndexList = pickerIndexList;
  21.   that.setData({
  22.    yearList,
  23.    monthList,
  24.    dayList,
  25.   })
  26. },
复制代码
第三步:在选择的过程中,选择器有个改变事件,当年份或者月份改变的时候,天数要随之变化
  1. //日期选择改变事件
  2. bindChangeDate: function (e) {
  3.   var that = this;
  4.   var pickerColumnList = e.detail.value;
  5.   that.setData({
  6.    dayList: that.getDayList(pickerColumnList[0], pickerColumnList[1]),
  7.    pickerIndexList: pickerColumnList,
  8.   })
  9. },
复制代码
有个样式的小问题:如选中行背景色写在picker-view控件中,则会出现滚动时背景色也会随着动,体验不好。所以我这里写了一个占位符,设置背景色,滚动选择时背景色就不会受到影响
  1. <!-- 选中行背景色 start-->
  2. <view class="top-background">
  3. <view></view>
  4. <view></view>
  5. <view></view>
  6. </view>
  7. <!-- 选中行背景色 end-->
复制代码
下面是全部代码~~
wxml:
  1. <!-- 自定义选择日期层 start -->
  2. <view class="date-layer" wx:if="{{isShowDateLayer}}" catchtouchmove="catchTouchMove">
  3. <view class="layer-box">
  4.   <view class="box-top">
  5.    <!-- 选中行背景色 start-->
  6.    <view class="top-background">
  7.     <view></view>
  8.     <view></view>
  9.     <view></view>
  10.    </view>
  11.    <!-- 选中行背景色 end-->
  12.    <picker-view indicator-class="picker-indicator" value="{{pickerIndexList}}" bindchange="bindChangeDate">
  13.     <picker-view-column>
  14.      <view wx:for="{{yearList}}" wx:key="index" class="{{pickerIndexList[0]==index?'txt-active':''}}">{{item}}年</view>
  15.     </picker-view-column>
  16.     <picker-view-column>
  17.      <view wx:for="{{monthList}}" wx:key="index" class="{{pickerIndexList[1]==index?'txt-active':''}}">{{item}}月</view>
  18.     </picker-view-column>
  19.     <picker-view-column>
  20.      <view wx:for="{{dayList}}" wx:key="index" class="{{pickerIndexList[2]==index?'txt-active':''}}">{{item}}日</view>
  21.     </picker-view-column>
  22.    </picker-view>
  23.   </view>
  24.   <view class="box-bottom">
  25.    <button class="btn-confirm" bindtap="bindConfirmDate">確定</button>
  26.    <button class="btn-cancel" bindtap="bindCancelDate">取消</button>
  27.   </view>
  28. </view>
  29. </view>
  30. <!-- 选择日期层 end -->
复制代码
js:
  1. /**
  2. * 页面的初始数据
  3. */
  4. data: {
  5.   pickerIndexList: [0, 0, 0],//日期选择器下标
  6.   isShowDateLayer: false,//是否显示日期弹层
  7.   txtDate: '請选择提貨日期',//选中日期
  8.   isSeltDate: false,//是否选择日期
  9. },
  10. // 截获竖向滑动
  11. catchTouchMove: function (res) {
  12.   return true;
  13. },
  14. //获取天数列表
  15. getDayList: function (year, month) {
  16.   var that = this;
  17.   var dayList;
  18.   switch (month + 1) {
  19.    case 1:
  20.    case 3:
  21.    case 5:
  22.    case 7:
  23.    case 8:
  24.    case 10:
  25.    case 12: dayList = that.getDayNum(31);
  26.     break;
  27.    case 4:
  28.    case 6:
  29.    case 9:
  30.    case 11: dayList = that.getDayNum(30);
  31.     break;
  32.    case 2: dayList = that.getDayNum((that.data.yearList[year] % 4 == 0 && that.data.yearList[year] % 100 != 0 || that.data.yearList[year] % 400 == 0) ? 29 : 28);
  33.     break;
  34.   }
  35.   return dayList;
  36. },
  37. //获取天数
  38. getDayNum: function (num) {
  39.   var dayList = [];
  40.   for (var i = 1; i <= num; i++) {
  41.    dayList.push(i);
  42.   }
  43.   return dayList;
  44. },
  45. //打开选择日期弹层
  46. bindOpenDateLayer: function (e) {
  47.   var that = this;
  48.   var pickerIndexList = that.data.pickerIndexList;
  49.   that.setData({
  50.    isShowDateLayer: !that.data.isShowDateLayer,
  51.    dayList: that.getDayList(pickerIndexList[0], pickerIndexList[1]),
  52.   })
  53. },
  54. //日期选择改变事件
  55. bindChangeDate: function (e) {
  56.   var that = this;
  57.   var pickerColumnList = e.detail.value;
  58.   that.setData({
  59.    dayList: that.getDayList(pickerColumnList[0], pickerColumnList[1]),
  60.    pickerIndexList: pickerColumnList,
  61.   })
  62. },
  63. //选择日期弹层确定按钮
  64. bindConfirmDate: function (e) {
  65.   var that = this;
  66.   var pickerIndexList = that.data.pickerIndexList;
  67.   var txtDate = that.data.yearList[pickerIndexList[0]] + '-' + that.data.monthList[pickerIndexList[1]] + '-' + that.data.dayList[pickerIndexList[2]];
  68.   that.setData({
  69.    isShowDateLayer: false,
  70.    pickerIndexList,
  71.    txtDate,
  72.    isSeltDate: true,
  73.   })
  74. },
  75. //选择日期弹层取消按钮
  76. bindCancelDate: function (e) {
  77.   var that = this;
  78.   var pickerIndexList = that.data.pickerIndexList;
  79.   that.setData({
  80.    isShowDateLayer: !that.data.isShowDateLayer,
  81.   })
  82.   var yearList = that.data.yearList;
  83.   var monthList = that.data.monthList;
  84.   var txtDate = that.data.txtDate;
  85.   var yearIndex = yearList.findIndex(o => o == parseInt(txtDate.slice(0, txtDate.indexOf('-'))));//年份下标
  86.   var monthIndex = monthList.findIndex(o => o == parseInt(txtDate.slice(txtDate.indexOf('-') + 1, txtDate.lastIndexOf('-'))));//月份下标
  87.   var dayList = that.getDayList(yearIndex, monthIndex);//天数
  88.   if (that.data.isSeltDate) {//选择过日期
  89.    if (txtDate == (yearList[pickerIndexList[0]] + '-' + monthList[pickerIndexList[1]] + '-' + dayList[pickerIndexList[2]]))
  90.     that.setData({ pickerIndexList })
  91.    else
  92.     that.setData({ pickerIndexList: [yearIndex, monthIndex, dayList.findIndex(o => o == parseInt(txtDate.slice(txtDate.lastIndexOf('-') + 1, txtDate.length)))] })
  93.   } else {//未选择过日期
  94.    that.setData({
  95.     pickerIndexList: app.globalData.dateIndexList,
  96.    })
  97.   }
  98. },
  99. //阻止冒泡事件
  100. catchLayer: function (e) { },
  101. //獲取當前日期
  102. getCurrentDate: function (e) {
  103.   var that = this;
  104.   var yearList = [], monthList = [], dayList = [];
  105.   for (var i = new Date().getFullYear(); i <= 2050; i++) {//年份
  106.    yearList.push(i);
  107.   }
  108.   for (var i = 1; i <= 12; i++) {//月份
  109.    monthList.push(i);
  110.   }
  111.   var myDate = new Date();
  112.   var currentYearIndex = yearList.findIndex(o => o == myDate.getFullYear());
  113.   var currentMonthIndex = monthList.findIndex(o => o == myDate.getMonth() + 1);
  114.   var dayList = that.getDayList(currentYearIndex, currentMonthIndex);//天
  115.   var currentDayIndex = dayList.findIndex(o => o == myDate.getDate());
  116.   var pickerIndexList = that.data.pickerIndexList;
  117.   pickerIndexList[0] = currentYearIndex;
  118.   pickerIndexList[1] = currentMonthIndex;
  119.   pickerIndexList[2] = currentDayIndex;
  120.   app.globalData.dateIndexList = pickerIndexList;
  121.   that.setData({
  122.    yearList,
  123.    monthList,
  124.    dayList,
  125.   })
  126. },
  127. /**
  128. * 生命周期函数--监听页面加载
  129. */
  130. onLoad: function (options) {
  131.   var that = this;
  132.   that.getCurrentDate();//獲取當前時間
  133.   that.setData({
  134.    pickerIndexList: that.data.pickerIndexList
  135.   })
  136. },
复制代码
wxss:
  1. /* 日期选择弹框 start */
  2. .main .date-layer {
  3. height: 100%;
  4. width: 100%;
  5. background: rgba(0, 0, 0, 0.65);
  6. position: fixed;
  7. top: 0;
  8. z-index: 20;
  9. }
  10. .date-layer .layer-box {
  11. position: fixed;
  12. bottom: 0;
  13. width: 100%;
  14. background: #fff;
  15. border-top-left-radius: 24rpx;
  16. border-top-right-radius: 24rpx;
  17. }
  18. .date-layer .layer-box .box-top {
  19. padding: 30rpx 0;
  20. psition: relative;
  21. }
  22. .date-layer .layer-box picker-view {
  23. height: 120px;
  24. width: 88%;
  25. margin: 0 auto;
  26. }
  27. .date-layer .layer-box .picker-indicator {
  28. height: 40px;
  29. line-height: 40px;
  30. }
  31. .date-layer .layer-box picker-view-column view {
  32. line-height: 42px;
  33. text-align: center;
  34. width: 96%;
  35. margin: 0 auto;
  36. }
  37. .date-layer .layer-box .box-top .top-background {
  38. height: 80rpx;
  39. width: 88%;
  40. margin: 0 auto;
  41. position: absolute;
  42. top: 50%;
  43. left: 50%;
  44. transform: translate(-50%, -50%);
  45. display: flex;
  46. }
  47. .layer-box .box-top .top-background view {
  48. height: 100%;
  49. width: 96%;
  50. margin: 0 auto;
  51. background: rgba(195, 218, 49, 0.12);
  52. border-top: 2rpx solid #D9E87D;
  53. border-bottom: 2rpx solid #C3DA31;
  54. margin: 0 4rpx;
  55. box-sizing: border-box;
  56. }
  57. .date-layer .layer-box .box-bottom {
  58. display: flex;
  59. }
  60. .date-layer .layer-box .box-bottom button {
  61. margin: 0;
  62. padding: 0;
  63. width: 50%;
  64. border-radius: 0;
  65. border: none;
  66. background: #fff;
  67. height: 100rpx;
  68. line-height: 100rpx;
  69. font-size: 34rpx;
  70. border-top: 2rpx solid #D8D8D8;
  71. }
  72. .date-layer .layer-box .box-bottom .btn-confirm {
  73. border-right: 1rpx solid #D8D8D8;
  74. color: #C3DA31;
  75. }
  76. .date-layer .layer-box .box-bottom .btn-cancel {
  77. border-left: 1rpx solid #D8D8D8;
  78. color: #B1B1B4;
  79. }
  80. /* 日期选择弹框 end */
复制代码
介绍到这里就可以实现一个不管是自己还是设计图都想要的选择器啦
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行