taro ui 小步伐at-calendar日历组件自定义样式+选择范围日历瓦解处置惩罚 ...

打印 上一主题 下一主题

主题 519|帖子 519|积分 1557

taro ui 日历文档
目次
单选+标记时间:
效果:
template:
data:
methods:
日历--范围选择:
效果:
template:
data:
methods:
日历--隔断多选:使用标记日期实现不连续多选日期
效果:
template:
data:
mothods:
 css:



单选+标记时间:

效果:


template:

  1. <at-calendar class="zs-calendar" :marks="marks" @monthChange="monthChange" @dayClick="selectDate" />
复制代码
data:

  1. const currentDate = ref(dateFormat(Date.now(), 'YYYY-MM-DD'))
  2. const marks = ref([]) // 日历打点 [{ value: '2024-06-10' }, { value: '2024-06-12' }]
  3. // 日历前面会展示上个月的几个日期,在数据查询的时候,可以查询从上个月20号开始到下个月20号,确保展示出来的日期都有数据
  4. const startDate = ref(getSpecialDate(currentDate.value, 1, 0, 20))
  5. const endDate = ref(getSpecialDate(currentDate.value, 0, 1, 20))
复制代码
methods:

  1. // 日历上点击某个日期
  2. const selectDate = (data) => {
  3.   currentDate.value = data.value
  4. }
  5. // 切换月份时 更新开始日期 结束日期
  6. const monthChange = (date) => {
  7.   startDate.value = getSpecialDate(date, 1, 0, 20)
  8.   endDate.value = getSpecialDate(date, 0, 1, 20)
  9. }
  10. // 获取前n个月or下n个月的某天
  11. // date 参照日期 2024-06-17,lastMonth前n个月 || nextMonth下n个月,day 20号
  12. // getLastMonthTwentieth('2024-06-17', 1, 0, 20) 获取上个月20号
  13. // getLastMonthTwentieth('2024-06-17', 0, 1, 6) 获取下个月6号
  14. export const getSpecialDate = (date, lastMonth, nextMonth, day) => {
  15.     const now = new Date(date); // 参照日期
  16.     const specialDate = nextMonth == 0 ? new Date(now.getFullYear(), now.getMonth() - lastMonth, day) : new Date(now.getFullYear(), now.getMonth() + nextMonth, day);
  17.     return dateFormat(specialDate, 'YYYY-MM-DD');
  18. }
  19. /**
  20. * 将时间戳转换为时间日期(如:2021-07-12 10:20:35)
  21. */
  22. export const dateFormat  = (timestamp, format) => {
  23.   if (String(timestamp).length === 10) {
  24.     timestamp = timestamp * 1000
  25.   }
  26.   let date = new Date(timestamp)
  27.   let Y = date.getFullYear()
  28.   let M = date.getMonth() + 1
  29.   let D = date.getDate()
  30.   let hour = date.getHours()
  31.   let min = date.getMinutes()
  32.   let sec = date.getSeconds()
  33.   if (format === 'YYYY') {
  34.     return Y // 2021
  35.   } else if (format === 'YYYY-MM') { // 2021-07
  36.     return Y + '-' + (M < 10 ? '0' + M : M)
  37.   } else if (format === 'YYYY-MM-DD') { // 2021-07-12
  38.     return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D)
  39.   } else if (format === 'HH:mm:ss') { // 10:20:35
  40.     return (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
  41.   } else if (format === 'YYYY-MM-DD HH:mm') { // 2021-07-12 10:20
  42.     return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min)
  43.   } else if (format === 'YYYY-MM-DD HH:mm:ss') { // 2021-07-12 10:20:35
  44.     return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
  45.   } else {
  46.     return '--'
  47.   }
  48. }
复制代码

日历--范围选择:

效果:


template:

  1. <at-calendar class="zs-calendar" :min-date="minDate" :marks="marks" :isMultiSelect="true" :currentDate="multiCurrentDate" @monthChange="monthChange" @selectDate="onSelectDate" />
复制代码
data:

  1. // 多选日历 日期
  2. let multiCurrentDate = ref({ start: dateFormat(Date.now(), 'YYYY-MM-DD') })
  3. // 多选 最小能选择的日期 明天
  4. const minDate = ref(dateFormat(new Date(new Date().getTime() + 24 * 60 * 60 * 1000), 'YYYY-MM-DD'))
  5. // 日历打点 [{ value: '2024-06-10' }, { value: '2024-06-12' }]
  6. const marks = ref()
复制代码
methods:

  1. // 多选日历 方法
  2. const onSelectDate = (selectedDates) => {
  3.   // 这块很重要!!不写会崩溃
  4.   // 点击的开始日期 = 上次的开始日期 or 点击的开始日期 = 上次的结束日期 ==》 重置选择范围
  5.   if (multiCurrentDate.value.start == selectedDates.value.start || multiCurrentDate.value.end == selectedDates.value.start) {
  6.     multiCurrentDate.value = { start: selectedDates.value.start }
  7.   }
  8.   // 点击的日期 有开始和结束
  9.   if (selectedDates.value.end) {
  10.     multiCurrentDate.value = selectedDates.value
  11.   }
  12.   // 无结束日期 =》重置dataList 重置
  13.   if (!selectedDates.value.end) { }
  14. }
复制代码

日历--隔断多选:使用标记日期实现不连续多选日期

效果:


template:

  1. <at-calendar class="zs-calendar zs-calendar-multi" :currentDate="currentDateMulti" :min-date="minDate" :marks="selectDataList" @dayClick="selectDateMulti" />
复制代码
data:

  1. // 多选 日历 当前日期
  2. const currentDateMulti = ref(dateFormat(Date.now(), 'YYYY-MM-DD'))
  3. // 多选 不连续 多选 选择的日期 [{ value: '2024-06-10' }, { value: '2024-06-12' }]
  4. const selectDataList = ref([])
  5. // 多选 最小能选择的日期 明天
  6. const minDate = ref(dateFormat(new Date(new Date().getTime() + 24 * 60 * 60 * 1000), 'YYYY-MM-DD'))
复制代码
mothods:

  1. const selectDateMulti = (data) => {
  2.   // 先赋值,防止dom不变
  3.   currentDateMulti.value = data.value
  4.   const list = JSON.parse(JSON.stringify(selectDataList.value))
  5.   const index = list.findIndex(item => { return item.value == data.value })
  6.   // 存在且就剩这2个
  7.   if (list.length == 2 && index > -1) {
  8.     Taro.showToast({
  9.       title: '请至少选择2个日期',
  10.       icon: 'none',
  11.       duration: 2000
  12.     })
  13.   }
  14.   // 不存在
  15.   if (index == -1) {
  16.     list.push({ value: data.value })
  17.     dataList.value.push(data.value)
  18.     setTimeout(() => {
  19.       currentDateMulti.value = data.value
  20.     }, 10)
  21.   }
  22.   // 存在 且剩多个
  23.   if (list.length > 2 && index > -1) {
  24.     list.splice(index, 1)
  25.     dataList.value.splice(index, 1)
  26.     setTimeout(() => {
  27.       currentDateMulti.value = list[0].value
  28.       currentDateMulti.value = list[list.length - 1].value
  29.     }, 10)
  30.   }
  31.   selectDataList.value = JSON.parse(JSON.stringify(list))
  32. }
复制代码

 css:

  1. // @import "./zs-style.scss";
  2. $zs-color-primary:#4264E2;
  3. .zs-calendar .at-calendar__controller {
  4.     justify-content: space-between;
  5.     padding-left: 32px;
  6.     padding-right: 32px;
  7. }
  8. .zs-calendar .at-calendar__list.flex {
  9.     color: #333333;
  10. }
  11. .zs-calendar .at-calendar__list.flex .flex__item-extra .extra-marks .mark {
  12.     background-color: $zs-color-primary;
  13.     color: $zs-color-primary;
  14. }
  15. .zs-calendar .at-calendar__list.flex .flex__item--selected .extra-marks .mark {
  16.     background-color: white;
  17.     color: white;
  18. }
  19. .zs-calendar .at-calendar__list.flex .flex__item--selected-head.flex__item--selected-tail .flex__item-container {
  20.     background-color: $zs-color-primary;
  21. }
  22. .zs-calendar .at-calendar__list.flex .flex__item--today {
  23.     color: $zs-color-primary;
  24. }
  25. .zs-calendar .at-calendar__list.flex .flex__item--selected {
  26.     color: white;
  27.     background-color: $zs-color-primary;
  28. }
  29. .zs-calendar .at-calendar__list.flex .flex__item--selected-head.flex__item--selected-tail {
  30.     background-color: transparent;
  31. }
  32. .zs-calendar .at-calendar__list.flex .flex__item:nth-child(7n).flex__item--now:not(.flex__item--selected) {
  33.     color: #aaaaaa;
  34. }
  35. .zs-calendar .at-calendar__list.flex .flex__item:nth-child(7n+1).flex__item--now:not(.flex__item--selected) {
  36.     color: #aaaaaa;
  37. }
  38. // 多选样式 -- 浅蓝色背景圆点
  39. .zs-calendar-multi .at-calendar__list.flex .flex__item-extra .extra-marks {
  40.     bottom: 3px;
  41.     z-index: -1;
  42. }
  43. .zs-calendar-multi .at-calendar__list.flex .flex__item-extra .extra-marks .mark {
  44.     background-color: #eef7ff;
  45.     // color: #eef7ff;
  46.     color: transparent;
  47.     // color: azure;
  48.     width: 70px;
  49.     height: 70px;
  50. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

前进之路

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表