首先,Ant-Design-Vue
和Element UI
都是不支持在日期選擇時,日曆中的年月切換時拋出事件的。當我們需要自定義渲染日期時,沒有這個事件的監聽,我們並不知道年月變了,就無法向後端獲取標記的日期。
解決辦法
1、在DOM中監聽年月切換四個按鈕(不推薦)
2、監聽年月文本節點的變化,關鍵事件DOMCharacterDataModified
。
年月切換時,需要從後端獲取日期數據,所以最好增加遮罩和loading,但是官方依然不支持,所以用spin組件,然後手動取日曆的位置,蓋上去達到這個效果。
<a-date-picker
suffixIcon=" "
:defaultValue="moment()"
:disabledDate="disabledDate"
@openChange="datePanelChange"
:allowClear="false">
<template slot="dateRender" slot-scope="current, today">
<div :class="['ant-calendar-date', {'work-day': isWorkDay(current)}]"
@click="updateDate(current)">{{current.format('DD')}}</div>
</template>
</a-date-picker>
// datePanelChange方法爲openChange事件的執行函數
datePanelChange (isShow) {
if (isShow) {
setTimeout(() => {
const dateDom = document.querySelector('.ant-calendar-ym-select')
dateDom.addEventListener('DOMCharacterDataModified', () => {
this.getDatePanelStyle()
this.dateSpinning = true
const year = document.querySelector('.ant-calendar-year-select').innerText.replace('年', '')
let month = document.querySelector('.ant-calendar-month-select').innerText.replace('月', '')
month = month < 10 ? '0' + month : month
this.getWorkDay(year + '-' + month)
})
}, 0)
}
},