声明
bug:由于此篇博客是在bindcolumnchange事件中做的值的改变处理,因此会出现当你选择时,没有点击确定,直接取消返回后,会发现选择框的值依然改变。
造成原因:这一点就是由于在bindcolumnchange事件做的值改变处理造成。
处理方法:如果需要确定后再改变值,请将bindcolumnchange事件中的处理操作放到bindchange事件中。
效果图
实现原理
利用微信小程序的picker组件的多列选择器实现!
WXML
1 <view class="tui-picker-content"> 2 <view class="tui-picker-name">时间选择器(选择时分)</view> 3 <picker mode="time" value="{{time}}" start="09:00" end="17:30" bindchange="changeTime"> 4 <view class="tui-picker-detail"> 5 午饭时间: {{time}} 6 </view> 7 </picker> 8 </view> 9 10 <view class="tui-picker-content"> 11 <view class="tui-picker-name">日期选择器(选择年月日)</view> 12 <picker mode="date" value="{{date}}" start="2017-10-01" end="2017-10-08" bindchange="changeDate"> 13 <view class="tui-picker-detail"> 14 国庆出游: {{date}} 15 </view> 16 </picker> 17 </view> 18 19 <view class="tui-picker-content"> 20 <view class="tui-picker-name">日期时间选择器(精确到秒)</view> 21 <picker mode="multiSelector" value="{{dateTime}}" bindchange="changeDateTime" bindcolumnchange="changeDateTimeColumn" range="{{dateTimeArray}}"> 22 <view class="tui-picker-detail"> 23 选择日期时间: {{dateTimeArray[0][dateTime[0]]}}-{{dateTimeArray[1][dateTime[1]]}}-{{dateTimeArray[2][dateTime[2]]}} {{dateTimeArray[3][dateTime[3]]}}:{{dateTimeArray[4][dateTime[4]]}}:{{dateTimeArray[5][dateTime[5]]}} 24 </view> 25 </picker> 26 </view> 27 <view class="tui-picker-content"> 28 <view class="tui-picker-name">日期时间选择器(精确到分)</view> 29 <picker mode="multiSelector" value="{{dateTime1}}" bindchange="changeDateTime1" bindcolumnchange="changeDateTimeColumn1" range="{{dateTimeArray1}}"> 30 <view class="tui-picker-detail"> 31 选择日期时间: {{dateTimeArray1[0][dateTime1[0]]}}-{{dateTimeArray1[1][dateTime1[1]]}}-{{dateTimeArray1[2][dateTime1[2]]}} {{dateTimeArray1[3][dateTime1[3]]}}:{{dateTimeArray1[4][dateTime1[4]]}} 32 </view> 33 </picker> 34 </view>
JS
1 var dateTimePicker = require(‘../../utils/dateTimePicker.js‘); 2 3 Page({ 4 data: { 5 date: ‘2018-10-01‘, 6 time: ‘12:00‘, 7 dateTimeArray: null, 8 dateTime: null, 9 dateTimeArray1: null, 10 dateTime1: null, 11 startYear: 2000, 12 endYear: 2050 13 }, 14 onLoad(){ 15 // 获取完整的年月日 时分秒,以及默认显示的数组 16 var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear); 17 var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear); 18 // 精确到分的处理,将数组的秒去掉 19 var lastArray = obj1.dateTimeArray.pop(); 20 var lastTime = obj1.dateTime.pop(); 21 22 this.setData({ 23 dateTime: obj.dateTime, 24 dateTimeArray: obj.dateTimeArray, 25 dateTimeArray1: obj1.dateTimeArray, 26 dateTime1: obj1.dateTime 27 }); 28 }, 29 changeDate(e){ 30 this.setData({ date:e.detail.value}); 31 }, 32 changeTime(e){ 33 this.setData({ time: e.detail.value }); 34 }, 35 changeDateTime(e){ 36 this.setData({ dateTime: e.detail.value }); 37 }, 38 changeDateTime1(e) { 39 this.setData({ dateTime1: e.detail.value }); 40 }, 41 changeDateTimeColumn(e){ 42 var arr = this.data.dateTime, dateArr = this.data.dateTimeArray; 43 44 arr[e.detail.column] = e.detail.value; 45 dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]); 46 47 this.setData({ 48 dateTimeArray: dateArr, 49 dateTime: arr 50 }); 51 }, 52 changeDateTimeColumn1(e) { 53 var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1; 54 55 arr[e.detail.column] = e.detail.value; 56 dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]); 57 58 this.setData({ 59 dateTimeArray1: dateArr, 60 dateTime1: arr 61 }); 62 } 63 })
外部JS,dateTimePicker.js的引入
1 function withData(param){
2 return param < 10 ? ‘0‘ + param : ‘‘ + param;
3 }
4 function getLoopArray(start,end){
5 var start = start || 0;
6 var end = end || 1;
7 var array = [];
8 for (var i = start; i <= end; i++) {
9 array.push(withData(i));
10 }
11 return array;
12 }
13 function getMonthDay(year,month){
14 var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;
15
16 switch (month) {
17 case ‘01‘:
18 case ‘03‘:
19 case ‘05‘:
20 case ‘07‘:
21 case ‘08‘:
22 case ‘10‘:
23 case ‘12‘:
24 array = getLoopArray(1, 31)
25 break;
26 case ‘04‘:
27 case ‘06‘:
28 case ‘09‘:
29 case ‘11‘:
30 array = getLoopArray(1, 30)
31 break;
32 case ‘02‘:
33 array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
34 break;
35 default:
36 array = ‘月份格式不正确,请重新输入!‘
37 }
38 return array;
39 }
40 function getNewDateArry(){
41 // 当前时间的处理
42 var newDate = new Date();
43 var year = withData(newDate.getFullYear()),
44 mont = withData(newDate.getMonth() + 1),
45 date = withData(newDate.getDate()),
46 hour = withData(newDate.getHours()),
47 minu = withData(newDate.getMinutes()),
48 seco = withData(newDate.getSeconds());
49
50 return [year, mont, date, hour, minu, seco];
51 }
52 function dateTimePicker(startYear,endYear,date) {
53 // 返回默认显示的数组和联动数组的声明
54 var dateTime = [], dateTimeArray = [[],[],[],[],[],[]];
55 var start = startYear || 1978;
56 var end = endYear || 2100;
57 // 默认开始显示数据
58 var defaultDate = date ? [...date.split(‘ ‘)[0].split(‘-‘), ...date.split(‘ ‘)[1].split(‘:‘)] : getNewDateArry();
59 // 处理联动列表数据
60 /*年月日 时分秒*/
61 dateTimeArray[0] = getLoopArray(start,end);
62 dateTimeArray[1] = getLoopArray(1, 12);
63 dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
64 dateTimeArray[3] = getLoopArray(0, 23);
65 dateTimeArray[4] = getLoopArray(0, 59);
66 dateTimeArray[5] = getLoopArray(0, 59);
67
68 dateTimeArray.forEach((current,index) => {
69 dateTime.push(current.indexOf(defaultDate[index]));
70 });
71
72 return {
73 dateTimeArray: dateTimeArray,
74 dateTime: dateTime
75 }
76 }
77 module.exports = {
78 dateTimePicker: dateTimePicker,
79 getMonthDay: getMonthDay
80 }
总结
1、将初始化列表以及初始化默认显示的数组放到dateTimePicker.js,防止页面逻辑太乱,而且可以多处使用;
2、判断是否为闰年,在三木表达式中,必须将能别400整除放在前边,因为或运算只要一个条件满足,就会返回true,不会执行后续表达式;
3、switch case的合并方法需要注意格式;
4、如果只需要联动列表更新,二结果展示栏不更新,则在changeDateTimeColumn函数中只更新dateTimeArray的值。