内部时间组件设置默认值

This commit is contained in:
stt
2025-11-06 14:24:15 +08:00
parent b5aff1a837
commit 593f2e2c66
4 changed files with 205 additions and 19 deletions

View File

@@ -1,13 +1,26 @@
<template>
<div style="width: 540px">
<el-select v-model.trim="interval" style="min-width: 90px; width: 90px; margin-right: 10px"
@change="timeChange">
<el-select
v-model.trim="interval"
style="min-width: 90px; width: 90px; margin-right: 10px"
@change="timeChange"
>
<el-option v-for="item in timeOptions" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
<el-date-picker v-model.trim="timeValue" type="daterange" :disabled="disabledPicker"
style="width: 220px; margin-right: 10px" unlink-panels :clearable="false" range-separator=""
start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" :shortcuts="shortcuts" />
<el-date-picker
v-model.trim="timeValue"
type="daterange"
:disabled="disabledPicker"
style="width: 220px; margin-right: 10px"
unlink-panels
:clearable="false"
range-separator=""
start-placeholder="开始日期"
end-placeholder="结束日期"
value-format="YYYY-MM-DD"
:shortcuts="shortcuts"
/>
<el-button :disabled="backDisabled" type="primary" :icon="DArrowLeft" @click="preClick"></el-button>
<el-button type="primary" :icon="VideoPause" @click="nowTime">当前</el-button>
<el-button :disabled="preDisabled" type="primary" :icon="DArrowRight" @click="next"></el-button>
@@ -21,13 +34,19 @@ import { ref, onMounted, nextTick, watch } from 'vue'
interface Props {
nextFlag?: boolean
theCurrentTime?: boolean
initialInterval?: number
initialTimeValue?: any
}
const props = withDefaults(defineProps<Props>(), {
nextFlag: false,
theCurrentTime: true
theCurrentTime: true,
initialInterval: 3,
initialTimeValue: undefined
})
const emit = defineEmits(['change'])
const interval = ref(3)
const timeFlag = ref(1)
const count = ref(0)
@@ -72,8 +91,27 @@ const shortcuts = [
}
]
onMounted(() => {
// 使用传入的初始值
if (props.initialInterval !== undefined) {
interval.value = props.initialInterval
}
if (props.initialTimeValue) {
timeValue.value = props.initialTimeValue
}
timeChange(3)
})
// 添加统一的事件触发方法
const emitChange = () => {
nextTick(() => {
emit('change', {
interval: interval.value,
timeValue: timeValue.value,
timeFlag: timeFlag.value
})
})
}
// 选择时间范围
const timeChange = (e: number) => {
backDisabled.value = false
@@ -110,6 +148,8 @@ const timeChange = (e: number) => {
} else {
timeFlag.value = 1
}
// 触发 change 事件
emitChange()
}
// 当前
@@ -178,6 +218,9 @@ const preClick = () => {
// 判断向后键的状态
// var temp = NowgetEndTime()
// timeStatus(temp, endTime)
// 触发 change 事件
emitChange()
}
//下一个
const next = () => {
@@ -383,7 +426,6 @@ const next = () => {
if (year >= presentY && !props.nextFlag) {
startTime = presentY + '-01-01'
if (presentM < 10) {
if (presentD < 10) {
endTime = presentY + '-0' + presentM + '-0' + presentD
} else {
@@ -400,18 +442,31 @@ const next = () => {
startTime = year + '-01-01'
endTime = year + '-12-31'
}
}
if (!props.nextFlag) {
if (new Date(endTime + ' 00:00:00').getTime() >= new Date(window.XEUtils.toDateString(new Date(), 'yyyy-MM-dd ') + ' 00:00:00').getTime()) {
if (
new Date(endTime + ' 00:00:00').getTime() >=
new Date(window.XEUtils.toDateString(new Date(), 'yyyy-MM-dd ') + ' 00:00:00').getTime()
) {
preDisabled.value = true
}
}
timeValue.value = [startTime, endTime]
// 触发 change 事件
emitChange()
}
// 监听值变化并触发事件
watch(
[interval, timeValue],
() => {
emitChange()
},
{ deep: true }
)
const setTime = (flag = 0, e = 0) => {
let dd = window.XEUtils.toDateString(new Date().getTime() - e * 3600 * 1000 * 24, 'dd')