Ant-Design-Vue日期选择中日历年月切换事件监听

首先,Ant-Design-VueElement 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)
      }
    },
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值