<el-radio-group
v-model="selectedRange"
aria-label="item label position"
class="custom-radio-group"
@change="handleRangeChange"
>
<el-radio-button value="全部">全部</el-radio-button>
<el-radio-button value="今日">今日</el-radio-button>
<el-radio-button value="近七天">近七天</el-radio-button>
<el-radio-button value="近一个月">近一个月</el-radio-button>
<el-radio-button value="自定义">自定义</el-radio-button>
</el-radio-group>
<el-date-picker
:disabled="selectedRange !== '自定义'"
style="flex-grow: inherit; width: 350px"
v-model="customDateRange"
type="daterange" <!-- 改为daterange类型,去掉时间选择 -->
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
format="YYYY-MM-DD" <!-- 只保留年月日格式 -->
value-format="YYYY-MM-DD" <!-- 只保留年月日格式 -->
@change="handleDateChange"
/>
// 时间控制
const selectedRange = ref('全部')
const customDateRange = ref([])
// 格式化日期为 YYYY-MM-DD 字符串(去掉时分秒)
const formatDate = (date) => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}` // 只返回年月日
}
const handleRangeChange = (value) => {
if (value !== '自定义') {
const now = new Date()
const start = new Date(now)
const end = new Date(now) // 单独定义结束时间
switch (value) {
case '今日':
// 今日
start.setHours(0, 0, 0, 0)
// 结束日期不需要设置时间,只保留日期
customDateRange.value = [formatDate(start), formatDate(end)]
handleSearch()
break
case '近七天':
// 7天前至今日
start.setDate(start.getDate() - 6)
start.setHours(0, 0, 0, 0)
customDateRange.value = [formatDate(start), formatDate(end)]
handleSearch()
break
case '近一个月':
// 30天前至今日
start.setMonth(start.getMonth() - 1)
start.setHours(0, 0, 0, 0)
customDateRange.value = [formatDate(start), formatDate(end)]
handleSearch()
break
case '全部':
customDateRange.value = []
handleSearch()
break
}
}
}
const handleDateChange = (dates) => {
console.log(dates)
if (dates && dates.length === 2) {
selectedRange.value = '自定义'
}
}
