-
Notifications
You must be signed in to change notification settings - Fork 15
/
datepicker-2.vue
360 lines (354 loc) · 8.55 KB
/
datepicker-2.vue
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<template>
<div class="vue-datepicker" @mouseout="endChoice" @mouseover="startMouseOver">
<input type="text" autocomplete="off" disableautocomplete :name="field" :id="field" :value="value" :placeholder="placeholder" @focus="startChoice" @keypress="startChoice" @blur="endChoice" ref="input">
<div class="vue-datepicker-panels" v-show="dayPanelIsShow || monthPanelIsShow">
<!--日期选择-->
<div class="vue-datepicker-panel" v-show="dayPanelIsShow">
<div class="vue-datepicker-month">
<a class="vue-datepicker-prev" @click="prevMonth"> < </a>
<span class="vue-datepicker-btn" @click="startChoiceMonth">{{ year }}年 {{ month+1 }}月</span>
<a class="vue-datepicker-next" @click="nextMonth"> > </a>
</div>
<table class="vue-datepicker-tb">
<thead>
<tr>
<th v-for="d in langConf.week">{{ d }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(m, mIndex) in monthDays" track-by="$index">
<td v-for="(d, dIndex) in m" track-by="$index" @click="choiceDay(d, $event)"
:class="classDay(d)">
<span>{{ d }}</span>
</td>
</tr>
</tbody>
</table>
</div>
<!--年月选择-->
<div class="vue-datepicker-panel" v-show="monthPanelIsShow">
<div class="vue-datepicker-month">
<a class="vue-datepicker-prev" @click="prevYear"> < </a>
<span>{{ year }}年</span>
<a class="vue-datepicker-next" @click="nextYear"> > </a>
</div>
<table class="vue-datepicker-tb2">
<col width="33%"/>
<col width="33%"/>
<col width="33%"/>
<tbody>
<tr v-for="season in monthArr" track-by="$index">
<td v-for="m in season" track-by="$index" @click="choiceMonth(m.id)"
:class="classMonth(m.id)">
<span>{{ m.name }}</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</template>
<script>
'use strict';
// 日期格式化输出
function dateFormat(y, m, d, fm) {
if (!fm) {
fm = 'yyyy-mm-dd';
}
m = ('0' + (parseInt(m) + 1)).slice(-2);
d = ('0' + d).slice(-2);
return fm.replace('yyyy', y)
.replace('YYYY', y)
.replace('mm', m)
.replace('MM', m)
.replace('DD', d)
.replace('dd', d);
}
// 比较两个日期大小,返回 -1 0 1
function dateCompare(date1, date2) {
if(date1.year == date2.year){
if(date1.day && date2.day) {
if(date1.month == date2.month){
return date1.day > date2.day ? 1 : (date1.day == date2.day ? 0 : -1);
}else {
return date1.month > date2.month ? 1 : -1;
}
}else {
return date1.month > date2.month ? 1 : (date1.month == date2.month ? 0 : -1);
}
}else {
return date1.year > date2.year ? 1: -1;
}
}
export default {
props: {
field: {
type: String,
default: ''
},
value: {
type: String,
default: '',
twoWay: true
},
format: {
type: String,
default: 'yyyy-mm-dd'
},
// 不能选今天
noToday: {
default: false
},
// 向前看,只能选今天及以后
forward: {
default: false
},
// 向后看
backward: {
default: false
},
placeholder: {
type: String,
default: ''
},
conf: {
type: Object,
default: function () {
return {};
}
}
},
data() {
let { value, conf} = this,
dateObj, year, month, day,
langConf = {
week: ['日', '一', '二', '三', '四', '五', '六'],
month: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
};
if (value) {
dateObj = new Date(value);
} else {
dateObj = new Date();
}
if (conf) {
langConf = Object.assign(langConf, conf);
}
year = dateObj.getFullYear();
month = dateObj.getMonth();
day = dateObj.getDate();
return {
langConf,
dayPanelIsShow: false,
monthPanelIsShow: false,
isMouseOver: false,
year,
month,
day
};
},
computed: {
// 日期二维数组(3*4),渲染用
monthArr(){
let {month} = this.langConf,
res = [];
for (let i = 0; i <= 3; i++) {
let temp = [];
for (let j = 1; j <= 3; j++) {
let id = i * 3 + j - 1;
temp.push({id, name: month[id]});
}
res.push(temp);
}
return res;
},
// 当前日期
curr(){
let {value, noToday} = this,
dateObj,
year="0000",
month="00",
day="00";
if (value) {
dateObj = new Date(value);
} else if(!noToday) {
dateObj = new Date();
}else{
return {year, month, day};
}
year = dateObj.getFullYear();
month = dateObj.getMonth();
day = dateObj.getDate();
return {year, month, day};
},
// 返回当前月的天数数组
monthDays() {
let {year, month} = this,
dayNum;
month++;
// 判断一个月有几天
if (month == 2) {
if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0)) {
dayNum = 29;
} else {
dayNum = 28;
}
} else {
if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
dayNum = 31;
} else {
dayNum = 30;
}
}
// 生成对应星期的日期数组
let firstDay = new Date(year, month - 1, 1).getDay(),
dayArr = new Array(6).fill(0).map(function () {
return new Array(7).fill('');
}),
row = 0,
col = firstDay;
for (let d = 1; d <= dayNum; d++) {
dayArr[row][col] = d;
if (col < 6) {
col++;
} else {
col = 0;
row++;
}
}
return dayArr;
},
today(){
let dateObj = new Date(),
year = dateObj.getFullYear(),
month = dateObj.getMonth(),
day = dateObj.getDate();
return {year, month, day};
}
},
methods: {
// 判断日期是否合法 dateObj格式{year,month,day}
dateIsValid(dateObj){
let {today, forward, backward, noToday} = this;
if(forward && dateCompare(today, dateObj) > 0){
return false;
}
if(backward && dateCompare(today, dateObj) < 0){
return false;
}
if(noToday && dateObj.day && dateCompare(today, dateObj) == 0){
return false;
}
return true;
},
// 年份+月份选择
startChoiceMonth(){
this.dayPanelIsShow = false;
this.monthPanelIsShow = true;
},
prevYear() {
let {year, forward, today} = this;
if (forward && today.year >= year) {
return false;
}
this.year--;
},
nextYear() {
let {year, backward, today} = this;
if (backward && today.year <= year) {
return false;
}
this.year++;
},
choiceMonth(m){
let {year} = this;
if (this.dateIsValid({year, month: m})) {
this.month = m;
this.dayPanelIsShow = true;
this.monthPanelIsShow = false;
}
},
// 样式
classMonth(m){
let that = this,
{month, year, curr} = that;
return {
'z-on': m == month && curr.year == year,
'z-existed': true,
'z-invalid': !that.dateIsValid({year, month:m})
};
},
classDay(d){
let that = this,
{month, year, curr} = that;
return {
'z-on': curr.day == d && curr.month == month && curr.year == year,
'z-existed': d != '',
'z-invalid': !that.dateIsValid({year, month, day:d})
};
},
// 月份+日期选择
startChoice(e) {
if (e && e.type == 'keypress') {
e.returnValue = false;
}
if(!this.dayPanelIsShow && !this.monthPanelIsShow){
this.dayPanelIsShow = true;
}
},
prevMonth() {
let {year ,month} = this;
if (this.dateIsValid({year, month: month-1})) {
if (month > 1) {
this.month--;
} else {
this.year--;
this.month = 11;
}
}
},
nextMonth() {
let {year ,month} = this;
if (this.dateIsValid({year, month: month+1})) {
if (month < 11) {
this.month++;
} else {
this.year++;
this.month = 0;
}
}
},
choiceDay(d) {
let {year, month, format} = this;
if (d && this.dateIsValid({year, month, day:d})) {
this.day = d;
this.$emit('input', dateFormat(year, month, d, format));
this.immEndChoice();
}
},
// 鼠标离开日期选择区域时超过一定时间,关闭日期面板
endChoice(e) {
let that = this,
inputEle = that.$refs.input;
if(e.type == 'mouseout') {
that.isMouseOver = false;
}
setTimeout(function() {
if (!that.isMouseOver && inputEle != document.activeElement) {
that.immEndChoice();
}
}, 300);
},
startMouseOver(){
this.isMouseOver = true;
},
// 立即关闭日期面板
immEndChoice(){
this.isMouseOver = true;
this.dayPanelIsShow = false;
this.monthPanelIsShow = false;
}
}
}
</script>
<style scoped> @import './datepicker.css'; </style>