<template> <div id="box"> <el-date-picker popper-class="data-style" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" :inline="true"> <template #default="{ date }"> <div class="date-wrapper"> <div :class="getCustomClass(date)"> {{ date.getDate() }} <span v-if="highlightedDates.includes(formatDate(date))" class="red-dot">已租出</span> </div> </div> </template> </el-date-picker> </div> </template> <script setup> import { ref } from 'vue' import { ElDatePicker } from 'element-plus' // 堆代码 duidaima.com // const selectedDate = ref('') const highlightedDates = ['2023-09-16', '2023-10-23', '2023-10-24', '2023-10-27', '2023-10-26', '2023-10-29', '2023-11-01', '2023-12-24', '2023-11-02', '2023-11-03', '2023-11-05', '2023-11-23'] const pickerOptions = { disabledDate: (time) => { const date = new Date(time) const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return highlightedDates.indexOf(`${year}-${month}-${day}`) === -1 }, } function 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}` } function getCustomClass(date) { const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') const day = String(date.getDate()).padStart(2, '0') const formattedDate = `${year}-${month}-${day}` if (highlightedDates.includes(formattedDate)) { return 'red-date' } return '' } </script> <!-- 因为日历组件还没有显示出来,所以我们无法通过深度监听来拿到日历弹层就需要另外写一个style样式表,没有样式隔离 --> <style lang="scss"> .data-style { // border: 1px solid red !important; // display: block !important; // 动态修改日历弹层的样式(不需要样式渗透)(如果不生效,记得添加 !important) } </style> <style lang="scss" scoped> .date-wrapper { position: relative; } .red-date { display: inline-block; border-radius: 50%; width: 20px; height: 20px; line-height: 20px; background-color: red; color: white; position: relative; } .red-dot { position: absolute; bottom: -18px; left: -4px; width: 100%; font-size: 10px; color: red; text-align: center; white-space: nowrap; } </style>注意:
因为日历组件还没有显示出来,所以我们无法通过深度监听来拿到日历弹层就需要另外写一个style样式表,没有样式隔离。
为了增强代码的可读性和可维护性,我们还可以添加一些样式表。其中包含了一些定义 red-date 样式的规则,以及一些用于修改日期选择器样式的规则。这些样式表将确保日期选择器和标记样式的正确显示。