【vue】【element-plus】 el-date-picker使用cell-class-name进行标记,type=year不生效解决方法

type=dete ,自定义cell-class-name打标记效果如下:

相关代码:
bash 复制代码
        <el-date-picker
            v-model="date"
            :clearable="false"
            :editable="false"
            :cell-class-name="cellClassName"
            type="date"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
        >
        </el-date-picker>


	//customDateArr : ['2025-04-01', '2025-04-23','2025-04-11']

      cellClassName:(date)=>{
        let that = this
        let nDate = parseTime(date, '{y}-{m}-{d}')//日期格式化方法
        // console.log(nDate)
        if (that.customDateArr.includes(nDate)) {
          return 'custom_date_class'; // 应用自定义样式类
        }
        return ''; // 其他日期不应用样式
      },
bash 复制代码
<style lang="scss">
.custom_date_class {
  span::after{
    content: "";
    position: absolute;
    width: 6px;
    height: 6px;
    background: var(--el-color-danger);
    border-radius: 50%;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
  }
}
</style>
type=year相关内容:

同样方法,当type=year时候,改成对应年份匹配后发现方法没有被调用,使用官方文档中的插槽方式(已升级到最新版)也无法实现。

最后通过焦点事件和面板更换事件直接操作dom元素进行强制赋值解决!!

bash 复制代码
<div class="select-center-left date-select">
        <el-date-picker
            popper-class="custom-year-picker"
            v-model="year"
            :clearable="false"
            :editable="false"
            type="year"
            value-format="YYYY"
            @focus="handleYearPickerOpen"
            @panel-change="handleYearPickerOpen"
            :disabled-date="disabledDate"
        />

const handleYearPickerOpen = () => {
  nextTick(() => {
    const pickerPanel = document.querySelector(' .custom-year-picker');
    // console.log(pickerPanel)
    if (pickerPanel) {
      const cells = pickerPanel.querySelectorAll('td');
      cells.forEach(cell => {
        // 示例:为 2023 年的单元格添加样式
        if (cell.textContent.includes('2023')||cell.textContent.includes('2019')) {
          cell.classList.add('custom_date_class');
        }else {
       		//不匹配需手动移除,否则切换面板后上次样式依旧存在
          	cell.classList.remove('custom_date_class')
        }
      });
    }
  });
};

类型为月份相关解决:element-ui的日期选择器cellClassName无效问题

相关推荐
sycmancia11 小时前
Qt——布局管理器(一)
前端·qt
peak_chan11 小时前
通过vue-virtual-scroller封装虚拟滚动el-select
前端·javascript·vue.js
小李子呢021111 小时前
前端八股Vue(7)---computed计算属性和watch侦听器
前端·javascript·vue.js
CCIE-Yasuo11 小时前
Win11-Microsoft Edge使用起来CPU飙升以及卡顿问题解决
前端·microsoft·edge·排故
吴声子夜歌11 小时前
ES6——对象的扩展详解
开发语言·javascript·es6
Ruihong12 小时前
Vue3 转 React:组件透传 Attributes 与 useAttrs 使用详解|VuReact 实战
vue.js·react.js
是江迪呀12 小时前
实时看大家都在干嘛?我靠一行监听函数,做了个轻互动小程序
前端·微信小程序
QCzblack12 小时前
BugKu BUUCTF ——Reverse
java·前端·数据库
gwjcloud12 小时前
基于linux下docker部署前端vue项目
前端·javascript·vue.js
小李子呢021112 小时前
前端八股CSS(1)---响应式布局的方法
前端·css