-
Notifications
You must be signed in to change notification settings - Fork 15
/
datepicker--2.vue
380 lines (372 loc) · 7.83 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<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" :key="mIndex">
<td v-for="(d, dIndex) in m" :key="dIndex" @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, sIndex) in monthArr" :key="sIndex">
<td v-for="(m, mIndex) in season" :key="mIndex" @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);
}
// 比较两个日期大小,如果date1大于date2返回true,否则false
function dateCompare(date1, date2) {
if (date1.day && date2.day) {
return date1.year > date2.year ||
(date1.year == date2.year && date1.month > date2.month) ||
(date1.year == date2.year && date1.month == date2.month && date1.day > date2.day);
} else {
return date1.year > date2.year || (date1.year == date2.year && date1.month > date2.month);
}
}
export default {
props: {
field: {
type: String,
default: ''
},
value: {
type: String,
default: ''
},
format: {
type: String,
default: 'yyyy-mm-dd'
},
// 向前看,只能选今天及以后
forward: {
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
} = this,
dateObj, year, month, day;
if (value) {
dateObj = new Date(value);
} else {
dateObj = new Date();
}
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: {
// 年份+月份选择
startChoiceMonth() {
this.dayPanelIsShow = false;
this.monthPanelIsShow = true;
},
prevYear() {
let {
year,
forward,
today
} = this;
if (forward && today.year >= year) {
return false;
}
this.year--;
},
nextYear() {
this.year++;
},
choiceMonth(m) {
let {
year,
today,
forward
} = this;
if (forward && (today.year > year || (today.year == year && today.month > m))) {
return false;
}
this.month = m;
this.startChoice();
},
// 样式
classMonth(m) {
let {
month,
year,
curr,
today,
forward
} = this;
return {
'z-on': m == month && curr.year == year,
'z-existed': true,
'z-invalid': forward && (today.year > year || (today.year == year && today.month > m))
};
},
classDay(d) {
let {
month,
year,
curr,
today,
forward
} = this,
isValid = forward && dateCompare(today, {
year,
month,
day: d
});
return {
'z-on': curr.day == d && curr.month == month && curr.year == year,
'z-existed': d != '',
'z-invalid': isValid
};
},
// 月份+日期选择
startChoice(e) {
if (e && e.type == 'keypress') {
e.returnValue = false;
}
this.dayPanelIsShow = true;
this.monthPanelIsShow = false;
},
prevMonth() {
let {
year,
month,
today,
forward
} = this;
if (forward && (today.year > year || (today.year == year && today.month >= month))) {
return false;
}
if (month > 1) {
this.month--;
} else {
this.year--;
this.month = 11;
}
},
nextMonth() {
let {
month
} = this;
if (month < 11) {
this.month++;
} else {
this.year++;
this.month = 0;
}
},
choiceDay(d) {
let {
year,
month,
format,
today,
forward
} = this;
if (d !== '' && !(forward && dateCompare(today, {
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.dayPanelIsShow = false;
}
}, 300);
},
startMouseOver() {
this.isMouseOver = true;
},
// 立即关闭日期面板
immEndChoice() {
this.isMouseOver = false;
this.dayPanelIsShow = false;
}
}
}
</script>
<style scoped> @import './datepicker.css'; </style>