Fork me on GitHub

微信小程序-日历(选择日期、月份)

一、WXML

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
32
33
34
35
36
37
38
39

<!-- 日历 -->
<view class="gradient">
<view class="box">
<view class="spaceAroundCenter">
<view class="flex-item">
<view class="item-content" bindtap="doDay" data-key='left'>
<view class="glyphicon glyphicon-triangle-left">《</view>
</view>
</view>
<view class="flex-item item-content-current-day">
<view class="item-content">{{currentDate}}</view>
</view>
<view class="flex-item">
<view class="item-content" bindtap="doDay" data-key="right">
<view class="glyphicon glyphicon-triangle-right">》</view>
</view>
</view>
</view>

<view class="spaceAroundCenter">
<view>一</view>
<view>二</view>
<view>三</view>
<view>四</view>
<view>五</view>
<view>六</view>
<view>日</view>
</view>

<view class="spaceAroundCenter">
<view class="flex-item" wx:for="{{currentDayList}}" wx:for-index='key' wx:for-item="vo" wx:key="{{key}}">
<view id='{{key}}' class="item-content bk-color-day" wx:if="{{vo == currentDay}}">{{vo}}</view>
<view id='{{key}}' class="item-content bk-color-dayClick" wx:elif="{{key == currentClickKey && currentClickKey != '' && vo != ''}}">{{vo}}</view>
<view id='{{key}}' class="item-content" bindtap='onClickItem' wx:else>{{vo}}</view>
</view>
</view>
</view>
</view>

二、WXSS

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
32
33
34
35
36
37
38
39
.spaceAroundCenter {
display: flex;
flex-flow: row wrap;
align-items: center;
justify-content: space-around;
}

.box {
margin: 30rpx;
}

.flex-item {
/* flex-flow: nowrap;
flex-grow: 1;
flex-shrink: 1; */
width: 14.2%;
}

.item-content {
padding: 25rpx 0;
text-align: center;
}
/* 当前日期 */
.bk-color-day {
color: #fff;
border-radius: 50%;
background-color: #ed7e2d;
}

/* 当前选中日期 */
.bk-color-dayClick {
color: #fff;
border-radius: 50%;
background-color: #e5e5e5;
}

.item-content-current-day {
flex-grow: 2;
}

三、JS

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Page({
data: {
currentDate: "",
dayList: '',
currentDayList: '',
currentObj: '',
currentDay: '',
currentClickKey: '',
},
onLoad: function (options) {
var currentObj = this.getCurrentDayString()
this.setData({
currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
currentDay: currentObj.getDate(),
currentObj: currentObj
})
this.setSchedule(currentObj)
},
doDay: function (e) {
var that = this
var currentObj = that.data.currentObj
var Y = currentObj.getFullYear();
var m = currentObj.getMonth() + 1;
var d = currentObj.getDate();
var str = ''
if (e.currentTarget.dataset.key == 'left') {
m -= 1
if (m <= 0) {
str = (Y - 1) + '/' + 12 + '/' + d
} else {
str = Y + '/' + m + '/' + d
}
} else {
m += 1
if (m <= 12) {
str = Y + '/' + m + '/' + d
} else {
str = (Y + 1) + '/' + 1 + '/' + d
}
}
currentObj = new Date(str)
this.setData({
currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
currentObj: currentObj
})
this.setSchedule(currentObj);
},
getCurrentDayString: function () {
var objDate = this.data.currentObj
if (objDate != '') {
return objDate
} else {
var c_obj = new Date()
var a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
return new Date(a)
}
},
setSchedule: function (currentObj) {
var that = this
var m = currentObj.getMonth() + 1
var Y = currentObj.getFullYear()
var d = currentObj.getDate();
var dayString = Y + '/' + m + '/' + currentObj.getDate()
var currentDayNum = new Date(Y, m, 0).getDate()
var currentDayWeek = currentObj.getUTCDay() + 1
var result = currentDayWeek - (d % 7 - 1);
var firstKey = result <= 0 ? 7 + result : result;
var currentDayList = []
var f = 0
for (var i = 0; i < 42; i++) {
let data = []
if (i < firstKey - 1) {
currentDayList[i] = ''
} else {
if (f < currentDayNum) {
currentDayList[i] = f + 1
f = currentDayList[i]
} else if (f >= currentDayNum) {
currentDayList[i] = ''
}
}
}
that.setData({
currentDayList: currentDayList
})
},

// 设置点击事件
onClickItem: function(e) {

console.log(JSON.stringify((e)));
console.log('1111111111111111111111');

console.log(JSON.stringify((e.currentTarget)));
this.setData({
currentClickKey: e.currentTarget.id
});
}
})

------------- The End -------------

本文标题:微信小程序-日历(选择日期、月份)

文章作者:White

发布时间:2018年10月28日 - 22:10

最后更新:2018年11月01日 - 11:11

原始链接:http://yoursite.com/2018/10/28/WX-calendar/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。