1.實現(xiàn)效果
2.實現(xiàn)原理
mode='multiSelector'多列選擇器
自定義范圍數(shù)組range
3.實現(xiàn)代碼
<!--pages/jsCase/multiTime/index4.wxml-->
<view class="act_box">
<view class="act_item flex j_b">
<view class="nowp"><text class="require">*</text>
開始時間:
</view>
<picker mode="multiSelector" value="{{start_time}}" data-type="start_time" data-param='start_time_p' bindchange="changeDateTime" bindcolumnchange="changeDateTimeColumn" range="{{dateTimeArray}}">
<view class="flex-row act_right">
<text wx:if="{{!start_time_p}}">請選擇</text>
<text wx:else class="sel_text">{{start_time_p}}</text>
</view>
</picker>
</view>
<view class="act_item flex j_b">
<view class="nowp"><text class="require">*</text>
結(jié)束時間:
</view>
<picker mode="multiSelector" value="{{end_time}}" data-type="end_time" data-param='end_time_p' bindchange="changeDateTime" bindcolumnchange="changeDateTimeColumn" range="{{dateTimeArray}}">
<view class="flex-row act_right">
<text wx:if="{{!end_time_p}}">請選擇</text>
<text wx:else class="sel_text">{{end_time_p}}</text>
</view>
</picker>
</view>
</view>
/* pages/jsCase/multiTime/index4.wxss */
.act_item {
width: 702rpx;
background: #FFFFFF;
border-radius: 16rpx;
margin-bottom: 24rpx;
box-sizing: border-box;
padding: 24rpx;
position: relative;
font-size: 28rpx;
color: #4D4D4D;
font-weight: 500;
}
.require {
color: #E9543E;
}
.nowp {
white-space: nowrap;
margin-right: 15rpx;
}
.act_right {
display: flex;
justify-content: flex-end;
font-size: 28rpx;
color: #999;
}
.act_item picker {
flex: 1;
margin-left: 40rpx;
}
.act_box {
margin: 24rpx auto;
width: 710rpx;
}
.sel_text {
font-size: 28rpx;
font-weight: 500;
color: #4D4D4D;
}
// pages/jsCase/multiTime/index4.js
var dateTimePicker = require('../util/dateTimer.js');
Page({
data: {
end_time: '',
start_time: '',
dateTimeArray: '', //時間數(shù)組
startYear: 2000, //最小年份
endYear: 2050, // 最大年份
start_time_p: '', //顯示的開始時間
start_time_p: '', //顯示的結(jié)束時間
},
onLoad: function (options) {
// 獲取完整的年月日 時分秒,以及默認顯示的數(shù)組
var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
this.setData({
start_time: obj.dateTime,
end_time: obj.dateTime,
dateTimeArray: obj.dateTimeArray,
});
},
/**
* 選擇時間
* @param {*} e
*/
changeDateTime(e) {
let dateTimeArray = this.data.dateTimeArray,
{
type,
param
} = e.currentTarget.dataset;
this.setData({
[type]: e.detail.value,
[param]: dateTimeArray[0][e.detail.value[0]] + '-' + dateTimeArray[1][e.detail.value[1]] + '-' + dateTimeArray[2][e.detail.value[2]] + ' ' + dateTimeArray[3][e.detail.value[3]] + ':' + dateTimeArray[4][e.detail.value[4]] + ':' + dateTimeArray[5][e.detail.value[5]]
});
},
changeDateTimeColumn(e) {
var dateArr = this.data.dateTimeArray,
{
type
} = e.currentTarget.dataset,
arr = this.data[type];
arr[e.detail.column] = e.detail.value;
dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
this.setData({
dateTimeArray: dateArr,
[type]: arr
});
},
})
自定義range數(shù)組:
function withData(param) {
return param < 10 ? '0' + param : '' + param;
}
function getLoopArray(start, end) {
var start = start || 0;
var end = end || 1;
var array = [];
for (var i = start; i <= end; i++) {
array.push(withData(i));
}
return array;
}
function getMonthDay(year, month) {
var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
array = null;
switch (month) {
case '01':
case '03':
case '05':
case '07':
case '08':
case '10':
case '12':
array = getLoopArray(1, 31)
break;
case '04':
case '06':
case '09':
case '11':
array = getLoopArray(1, 30)
break;
case '02':
array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
break;
default:
array = '月份格式不正確,請重新輸入!'
}
return array;
}
function getNewDateArry() {
// 當(dāng)前時間的處理
var newDate = new Date();
var year = withData(newDate.getFullYear()),
mont = withData(newDate.getMonth() + 1),
date = withData(newDate.getDate()),
hour = withData(newDate.getHours()),
minu = withData(newDate.getMinutes()),
seco = withData(newDate.getSeconds());
return [year, mont, date, hour, minu, seco];
}
function dateTimePicker(startYear, endYear, date) {
// 返回默認顯示的數(shù)組和聯(lián)動數(shù)組的聲明
var dateTime = [],
dateTimeArray = [
[],
[],
[],
[],
[],
[]
];
var start = startYear || 1978;
var end = endYear || 2100;
// 默認開始顯示數(shù)據(jù)
var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
// 處理聯(lián)動列表數(shù)據(jù)
/*年月日 時分秒*/
dateTimeArray[0] = getLoopArray(start, end);
dateTimeArray[1] = getLoopArray(1, 12);
dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
dateTimeArray[3] = getLoopArray(0, 23);
dateTimeArray[4] = getLoopArray(0, 59);
dateTimeArray[5] = getLoopArray(0, 59);
dateTimeArray.forEach((current, index) => {
dateTime.push(current.indexOf(defaultDate[index]));
});
return {
dateTimeArray: dateTimeArray,
dateTime: dateTime
}
}
module.exports = {
dateTimePicker: dateTimePicker,
getMonthDay: getMonthDay
}