type=dete,自定義cell-class-name打標記效果如下:
相關代碼:
<el-date-pickerv-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 = thislet nDate = parseTime(date, '{y}-{m}-{d}')//日期格式化方法// console.log(nDate)if (that.customDateArr.includes(nDate)) {return 'custom_date_class'; // 應用自定義樣式類}return ''; // 其他日期不應用樣式},
<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元素進行強制賦值解決!!
<div class="select-center-left date-select"><el-date-pickerpopper-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無效問題