el-date-picker自定义只能选中当前月份和半年内月份等

需求:el-date-picker只能选中当前月期和当前月期往前半年,其他时间就禁用了不让选择了,因为没数据哈哈。当然也可以选择往前一年等。

一、效果

el-date-picker自定义只能选中当前月份和半年内月份等_第1张图片

 二、写个日期选择器

 :picker-options:日期选项

 value-format:选择后的格式

 @change:事件改变后触发的函数


        

三、data的值

      monthTimeData: [], // 绑定的日期值
      currentDate: new Date(), // 当前时间

四、computed

      this.currentDate.getMonth() - 4:注意!!!这个-4,就是当前月往前可选的几个月,

        -6就是不算上当前月往前可以选6个月,实在不懂可以自己测一下很简单的~

@change时间就是个函数,值就是选择后的值,这边我就不写了

  computed: {
    pickerOptions() {
      const startMonth = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() - 4
      );
      const endMonth = this.currentDate;
      
      return {
        disabledDate(time) {
          return time < startMonth || time > endMonth;
        },
      };
    },
  },

文章到此结束,希望对你有所帮助~

你可能感兴趣的:(vue,vue.js,elementui,前端)