element-ui 限制日期选择(datepicker)

需求场景如下:

  • 指定起止日期,后选的将会受到先选的限制
  • 不同的日期选择器,不过也存在关联关系

实现方法不难,利用了 change 事件,动态改变 picker-options 中的 disableDate 即可。

查看Demo

Template

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.3.8/lib/index.js"></script>
<div id="app">
<template>
  <div class="block">
    <span class="demonstration">起始日期</span>
    <el-date-picker v-model="startDate" type="date" placeholder="选择日期" :picker-options="pickerOptionsStart" @change="changeEnd">
    </el-date-picker>
  </div>
 
  <div class="block">
    <span class="demonstration">截止日期</span>
    <el-date-picker v-model="endDate" type="date" placeholder="选择日期" :picker-options="pickerOptionsEnd" @change="changeStart">
    </el-date-picker>
  </div>
</template>
</div>

Script

var Main = {
    data() {
      return {
        pickerOptionsStart: {},
        pickerOptionsEnd:{},
        startDate: '',
        endDate: '',
      };
    },
    methods:{
      changeStart (){
        this.pickerOptionsStart = Object.assign({},this.pickerOptionsStart,{
          disabledDate: (time) => {
            return time.getTime() > this.endDate
          }
        })
      },
      changeEnd (){
        this.pickerOptionsEnd = Object.assign({},this.pickerOptionsEnd,{
          disabledDate: (time) => {
            return time.getTime() < this.startDate
            }
        })
      }
    }
  };
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

Style

@import url("//unpkg.com/element-ui@2.3.8/lib/theme-chalk/index.css");

.block{
  margin-top:10px;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 11,475评论 1 9
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,658评论 2 45
  • 清晨醒来一路踏浪 像朝圣一般来到静修室 为自己的细胞沐浴 以光养护神经系统 以悦性温润腺体 以能量充沛胆魄 以热情...
    Fatehpal昱阅读 2,256评论 0 0
  • 已经立冬好些日子了,早已经把厚厚的棉衣和秋裤给穿了起来,我家院子里有两颗银杏树,是我老爸从浙江那边带过来的,这...
    十七岁的北斗星阅读 1,326评论 1 0
  • 雪夜校园中,街灯与飞雪,交织斑驳,纷纷扬扬。张斌 供图
    小欣吖阅读 825评论 0 0