IT评测·应用市场-qidao123.com技术社区

标题: 基于uniapp的日期选择器 [打印本页]

作者: 八卦阵    时间: 2025-4-7 07:59
标题: 基于uniapp的日期选择器
  1. <view class="content">
  2.     <view class="dateWrap" @click="getDate()">
  3.                 <u-icon name="calendar" color="#2979ff" size="24"></u-icon>
  4.                 <u-input class="iptCfg" placeholder="请选择考勤日期" :value="selectMonth" suffixIcon="arrow-down" readonly />
  5.     </view>
  6.         <scroll-view scroll-x="true" :scroll-left="scrollLeft">
  7.                 <view class="date-content">
  8.                         <view v-for="(dateItem, index) in dateList" :key="index" class="date-item" :class="{ selected: index == selectIndex }" @click="handleDateSelect(index)">
  9.                         <view v-if="dateItem.isToday" class="todayText">
  10.                                     <view class="circle"></view>今天
  11.                                 </view>
  12.                                 <view v-else>{{ dateItem.week }}</view>
  13.                                 <view class="day-number">{{ dateItem.day }}</view>
  14.                         </view>
  15.                 </view>
  16.         </scroll-view>
  17. </view>
  18. <u-datetime-picker :show="visible" v-model="value" mode="year-month" :maxDate="maxDate" :formatter="formatter" @confirm="confirm" @close="close" :closeOnClickOverlay="true"></u-datetime-picker>
  19. <script>
  20. mounted() {
  21.         this.initDate();
  22. },
  23. methods: {
  24.     //初始化日期,实现今天的日期高亮
  25.     initDate() {
  26.             const now = new Date();
  27.             this.currentYear = now.getFullYear();
  28.             this.currentMonth = now.getMonth() + 1;
  29.             this.selectMonth = this.$moment().format('YYYY-MM');
  30.             this.form.signDate = this.$moment().format('YYYY-MM-DD');               
  31.             this.generateDateList();//获取当前月份的所有日期
  32.     },
  33.     // 生成日期列表(当月所有日期)
  34.         generateDateList() {
  35.                 const currentMonthStart = this.$moment([this.currentYear, this.currentMonth - 1]);
  36.                 const today = this.$moment();
  37.                 const lastDay = currentMonthStart.clone().endOf('month').date();
  38.                 const dateList = [];
  39.                 for (let i = 1; i <= lastDay; i++) {
  40.                         const d = currentMonthStart.clone().date(i);
  41.                         const day = d.date();
  42.                         const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][d.day()];
  43.                         const isToday = d.isSame(today, 'day');
  44.                         dateList.push({
  45.                                 day,
  46.                                 week,
  47.                                 isToday,
  48.                                 isSelected: false
  49.                         });
  50.                 }
  51.                 this.dateList = dateList;
  52.                 this.calculateScrollLeft();//定位到当前日期所在位置
  53.         },
  54.     // 计算滚动位置,定位到今天的日期
  55.         calculateScrollLeft() {
  56.                 const todayIndex = this.dateList.findIndex(item => item.isToday);
  57.                 if (todayIndex !== -1) {
  58.                     this.selectIndex = todayIndex;//当前日期高亮赋值
  59.                     // 每个日期项宽度为 100rpx
  60.                     this.scrollLeft = todayIndex * 100;
  61.                     this.form.signDate = this.selectMonth + '-' + this.dateList[todayIndex].day;
  62.                     this.getList();//获取选择日期后的列表数据
  63.                 } else {
  64.                         this.selectIndex = 0;//非当前月份时,默认高亮第一天
  65.                         this.form.signDate = this.selectMonth + '-' + this.dateList[0].day;//列表传参
  66.                         this.getList();
  67.                 }
  68.         },
  69.     // 选择日--切换高亮
  70.         handleDateSelect(index) {
  71.                 this.selectIndex = index;
  72.                 this.dateList.forEach((item) => {
  73.                         item.isSelected = false;
  74.                 });
  75.                 this.dateList[index].isSelected = true;
  76.                 this.form.signDate = this.selectMonth + '-' + this.dateList[index].day;
  77.                 this.getList();
  78.         },
  79.         //选择月份
  80.         confirm(e) {
  81.                 this.currentYear = this.$moment(e.value).format('YYYY');
  82.                 this.currentMonth = this.$moment(e.value).format('MM');
  83.                 this.selectMonth = this.$moment(e.value).format('YYYY-MM');
  84.                 this.form.signDate = this.selectMonth + '-' + '01';
  85.                 this.visible = false;
  86.                 this.generateDateList();//选择月份,重新处理获取所属日
  87.         },
  88.     formatter(type, value) {
  89.             if (type === 'year') {
  90.                     return `${value}年`
  91.                 }
  92.                 if (type === 'month') {
  93.                         return `${value}月`
  94.                 }
  95.                 if (type === 'day') {
  96.                         return `${value}日`
  97.                 }
  98.                 return value
  99.         },
  100.         //日期弹框
  101.         getDate() {
  102.                 this.defaultDate = this.form.signDate ? this.form.signDate : '';
  103.                 this.visible = true;
  104.         },
  105.     //逻辑处理
  106.     getList() {
  107.     }
  108. }
  109. <style>
  110.     .date-content {
  111.                 width: 100%;
  112.                 display: flex;
  113.                 overflow-x: auto;
  114.                 margin-top: 10rpx;
  115.         }
  116.         .date-item {
  117.                 display: flex;
  118.                 flex-direction: column;
  119.                 width: 200rpx;
  120.                 align-items: center;
  121.                 margin-right: 20rpx;
  122.                 padding: 24rpx 20rpx;
  123.                 background-color: #ffffff;
  124.                 border-radius: 10rpx;
  125.                 transition: background-color 0.3s;
  126.         }
  127.         .todayText {
  128.                 position: relative;
  129.                 .circle {
  130.                         position: absolute;
  131.                         top: -14rpx;
  132.                         left: 20rpx;
  133.                         width: 10rpx;
  134.                         height: 10rpx;
  135.                         background-color: red;
  136.                         border-radius: 50%;
  137.                 }
  138.         }
  139.         .date-item.today {
  140.                 color: #fff;
  141.                 background-color: #1989fa;
  142.         }
  143.         .date-item.selected {
  144.                 color: #fff;
  145.                 background-color: #1989fa;
  146.         }
  147.         .date-item.today text {
  148.                 color: #ffffff;
  149.         }
  150.         .date-item text {
  151.                 color: #606266;
  152.                 font-size: 28rpx;
  153.                 margin-bottom: 8rpx;
  154.         }
  155.         .day-number {
  156.                 margin-top: 10rpx;
  157.                 width: 60rpx;
  158.                 text-align: center;
  159.                 // color: #303133;
  160.                 font-size: 36rpx;
  161.                 font-weight: bold;
  162.         }
  163. </style>
复制代码
效果展示

---------可封装组件,后期再美满--------

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4