<template>
|
<div>
|
<a-date-picker :getCalendarContainer="getCalendarContainer" v-model="startValue" :disabled-date="disabledStartDate"
|
:disabled-time="disabledStartTime" :show-time="showTime" :format="formatStr" placeholder="Start" :open="startOpen"
|
size="small" @openChange="handleStartOpenChange" @change="change(0)" />
|
-
|
<a-date-picker :getCalendarContainer="getCalendarContainer" v-model="endValue" :disabled-date="disabledEndDate"
|
:disabled-time="disabledEndTime" :show-time="showTime" :format="formatStr" placeholder="End" :open="endOpen"
|
size="small" @change="change(1)" @openChange="handleEndOpenChange" />
|
</div>
|
</template>
|
<script>
|
import moment from 'moment'
|
export default {
|
data() {
|
return {
|
startValue: null,
|
endValue: null,
|
startOpen: false,
|
endOpen: false,
|
};
|
},
|
props: {
|
value: {
|
required: true,
|
},
|
getCalendarContainer: {
|
required: true
|
},
|
showTime: {
|
type: Boolean,
|
default: true
|
},
|
},
|
model: {
|
prop: "value",
|
event: "change",
|
},
|
watch: {
|
value(newV) {
|
this.resultData = newV;
|
},
|
},
|
computed: {
|
resultData: {
|
get() {
|
return [this.startValue, this.endValue];
|
},
|
set(value) {
|
this.startValue = value[0];
|
this.endValue = value[1];
|
},
|
},
|
formatStr() {
|
return this.showTime ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD"
|
},
|
},
|
methods: {
|
range(start, end) {
|
const result = [];
|
for (let i = start; i < end; i++) {
|
result.push(i);
|
}
|
return result;
|
},
|
disabledStartDate(startValue) {
|
const endValue = this.endValue;
|
if (!startValue || !endValue) {
|
return false;
|
}
|
return startValue.valueOf() > endValue.valueOf();
|
},
|
disabledEndDate(endValue) {
|
const startValue = this.startValue;
|
if (!endValue || !startValue) {
|
return false;
|
}
|
// console.log(startValue.valueOf(), endValue.valueOf(), 'start, end');
|
return startValue.valueOf() >= endValue.valueOf();
|
},
|
disabledStartTime(startValue) {
|
let endValue = this.endValue;
|
if (!endValue || !startValue) {
|
return {
|
disabledHours: () => [],
|
disabledMinutes: () => [],
|
disabledSeconds: () => []
|
}
|
}
|
let startDate = startValue.format('YYYY-MM-DD');
|
let startH = startValue.hour(),
|
startM = startValue.minute(),
|
startS = startValue.second();
|
return {
|
disabledHours: () => this.range(0, 24).filter(v => moment(`${startDate} ${v}:${startM}:${startS}`).valueOf() > endValue.valueOf()),
|
disabledMinutes: () => this.range(0, 60).filter(v => moment(`${startDate} ${startH}:${v}:${startS}`).valueOf() > endValue.valueOf()),
|
disabledSeconds: () => this.range(0, 60).filter(v => moment(`${startDate} ${startH}:${startM}:${v}`).valueOf() > endValue.valueOf()),
|
};
|
},
|
disabledEndTime(endValue) {
|
let startValue = this.startValue;
|
if (!endValue || !startValue) {
|
return {
|
disabledHours: () => [],
|
disabledMinutes: () => [],
|
disabledSeconds: () => []
|
}
|
}
|
let endDate = endValue.format('YYYY-MM-DD');
|
let endH = endValue.hour(),
|
endM = endValue.minute(),
|
endS = endValue.second();
|
return {
|
disabledHours: () => this.range(0, 24).filter(v => moment(`${endDate} ${v}:${endM}:${endS}`).valueOf() < startValue.valueOf()),
|
disabledMinutes: () => this.range(0, 60).filter(v => moment(`${endDate} ${endH}:${v}:${endS}`).valueOf() < startValue.valueOf()),
|
disabledSeconds: () => this.range(0, 60).filter(v => moment(`${endDate} ${endH}:${endM}:${v}`).valueOf() < startValue.valueOf()),
|
};
|
},
|
handleStartOpenChange(open) {
|
this.startOpen = open;
|
if (!open && !this.endValue) {
|
this.endOpen = true;
|
}
|
},
|
handleEndOpenChange(open) {
|
this.endOpen = open;
|
// if (!open && !this.startValue) {
|
// this.startOpen = true;
|
// }
|
},
|
change(isEnd) {
|
// if (!this.startValue || !this.endValue) {
|
// return false;
|
// }
|
if (isEnd) {
|
// 如果结束时间比开始时间小
|
if (this.startValue && this.endValue.valueOf() < this.startValue.valueOf()) {
|
this.endValue = this.startValue;
|
}
|
} else {
|
if (this.endValue && this.startValue.valueOf() > this.endValue.valueOf()) {
|
this.startValue = this.endValue;
|
}
|
}
|
this.$emit("change", this.resultData);
|
},
|
},
|
};
|
</script>
|