網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
官方picker
可以設(shè)置有效時(shí)間,有效時(shí)間外的禁止選擇,但是無(wú)法隱藏,于是決定通過(guò)picker-view
和picker-view-column
來(lái)手寫(xiě)一個(gè),效果如圖:
需要注意的一些地方:
- 2月份,需要判斷是平/閏年;
- 大小月,30或31天;
- 還有就是選擇本年,控制顯示月份,選擇本月控制顯示日。
組件 wxml
<slot bindtap="handlePicker">slot>
<view hidden="{{!visible}}" class="date-picker-cover" bindtap="tapCancel">view>
<view class="date-picker" animation="{{animationData}}">
<view class="btn-area">
<view class="btn-cancel" bindtap="tapCancel">取消view>
<view class="btn-confirm" bindtap="tapConfirm">確定view>
view>
<picker-view class="picker-view" value="{{value}}" bindchange="changeDate">
<picker-view-column>
<view wx:for="{{years}}" wx:key="index" class="picker-view-column">{{item}}年view>
picker-view-column>
<picker-view-column>
<view wx:for="{{showDate.months}}" wx:key="index" class="picker-view-column">{{item > 9 ? item : '0' + item}}月view>
picker-view-column>
<picker-view-column>
<view wx:for="{{showDate.days}}" wx:key="index" class="picker-view-column">{{item > 9 ? item : '0' + item}}日view>
picker-view-column>
picker-view>
view>
組件 js
Component({
/**
* 組件的屬性列表
*/
properties: {
// 日期 格式 yyyy-MM-DD
dateValue: {
type: String,
observer: function (val) {
if (val) {
this.initDate(val)
}
}
}
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
visible: false,
years: [],
curDate: {
year: null,
month: null,
day: null
},
// 全部月、日
fullDate: {
months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
days: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
},
// 顯示月、日
showDate: {
months: [],
days: []
},
value: null,
dateArr: null,
formatDate: '',
animate: null,
animationData: null
},
pageLifetimes: {
show() {
let animate = wx.createAnimation({
duration: 400,
timingFunction: 'ease',
})
this.setData({
animate
})
}
},
/**
* 組件的方法列表
*/
methods: {
// 選擇日期
handlePicker() {
const slideUp = this.data.animate.bottom(0).step()
this.setData({
animationData: slideUp.export(),
// 彈出日期時(shí),顯示確定的時(shí)間
value: this.data.value,
visible: true
})
},
// 初始化日期
initDate(val) {
const date = new Date()
const curDate = {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate()
}
let years = []
for (let i = 1; i <= curDate.year; i++) {
years.push(i)
}
const value = val.split('-')
this.setData({
years,
curDate,
value: value.map(item => Number(item) - 1)
})
this.computedDate(value)
},
// 選擇日期
changeDate(val) {
let value = val.detail.value.map(item => item + 1)
this.computedDate(value)
const month = value[1] > 9 ? value[1] : '0' + value[1]
const day = value[2] > 9 ? value[2] : '0' + value[2]
this.setData({
dateArr: val.detail.value,
formatDate: `${value[0]}-${month}-${day}`,
})
},
// 計(jì)算月、日
computedDate (val) {
const { months: fullMonths, days: fullDays } = this.data.fullDate
const year = Number(val[0])
const month = Number(val[1])
// 閏年:能被 4 整除并且不能被 100 整除,或者被 400 整除
const leapYear = !(year % 4) && (year % 100) || !(year % 400) ? true : false
// 默認(rèn)大月 0:2月 1:小月
let monthType = null
if (month === 2) {
monthType = 0
} else if ([4, 6, 9, 11].includes(month)) {
monthType = 1
}
// 本年顯示 ≤ 本月
let months = year === this.data.curDate.year ? fullMonths.slice(0, this.data.curDate.month) : fullMonths
let days = fullDays
if (year === this.data.curDate.year && month === this.data.curDate.month) {
// 本年且本月,顯示 ≤ 本日
days = fullDays.slice(0, this.data.curDate.day)
} else {
if (monthType === 0) {
// 2月
days = leapYear ? fullDays.slice(0, 29) : fullDays.slice(0, 28)
} else if (monthType === 1) {
// 小月
days = fullDays.slice(0, 30)
}
}
this.setData({
['showDate.months']: months,
['showDate.days']: days
})
},
// 確定
tapConfirm() {
const slideDown = this.data.animate.bottom('-560rpx').step()
this.triggerEvent('change', this.data.formatDate)
this.setData({
value: this.data.dateArr,
animationData: slideDown.export(),
visible: false
})
},
// 取消
tapCancel() {
const slideDown = this.data.animate.bottom('-560rpx').step()
this.setData({
animationData: slideDown.export(),
visible: false
})
}
}
})
組件 wxss
.date-picker-cover {
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, .5);
position: fixed;
top: 0;
left: 0;
z-index: 99;
}
.date-picker {
width: 100%;
position: absolute;
left: 0;
bottom: 0;
background-color: #fff;
position: fixed;
bottom: -560rpx;
left: 0;
z-index: 100;
}
.date-picker .btn-area {
box-sizing: border-box;
width: 100%;
display: flex;
justify-content: space-between;
padding: 18rpx 30rpx;
border-bottom: 2rpx solid #f6f6f6;
font-size: 32rpx;
}
.date-picker .btn-area .btn-cancel {
color: #7f7f7f;
}
.date-picker .btn-area .btn-confirm {
color: #07c160;
}
.date-picker .picker-view {
width: 100%;
height: 480rpx;
}
.date-picker .picker-view .picker-view-column {
line-height: 34px;
text-align: center;
font-size: 32rpx;
}
調(diào)用組件
<view>
<date-picker dateValue="{{date}}" bindchange="changeDate">
<view class="date-content">選擇日期:{{date}}view>
date-picker>
view>
// js
Page({
data: {
date: '2021-12-02'
},
onLoad() {
},
changeDate(val) {
this.setData({
date: val.detail
})
}
})
// json
{
"usingComponents": {
"date-picker": "/components/date-picker/date-picker"
}
}
原文鏈接:https://blog.csdn.net/z291493823/article/details/122516359
相關(guān)推薦
- 2023-04-12 python字符串大小寫(xiě)轉(zhuǎn)換的三種方法_python
- 2022-03-23 Android?app啟動(dòng)圖適配方法實(shí)例_Android
- 2022-05-03 windows?server?2019開(kāi)啟iis服務(wù)器+tp5.1的完美配置運(yùn)行流程_win服務(wù)器
- 2022-10-01 Iptables防火墻limit模塊擴(kuò)展匹配規(guī)則詳解_安全相關(guān)
- 2022-04-04 react 報(bào)錯(cuò)Assign arrow function to a variable before
- 2024-04-08 nvm 在 Windows 上的使用
- 2022-09-26 React 函數(shù)式組件怎樣進(jìn)行優(yōu)化
- 2022-06-11 SQL常用日期查詢語(yǔ)句及顯示格式設(shè)置_MsSql
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支