美文网首页
el-date-picker

el-date-picker

作者: Hsugar | 来源:发表于2019-08-19 15:01 被阅读0次

    选择日期范围,用两个独立的选择器来控制,总觉得比较混乱。

    <el-date-picker
      ref="drawDate"
      :picker-options="pickerOptions"
      style="margin-left: 5px;"
      v-model="drawDate"
      value-format="yyyy-MM-dd"
      type="daterange"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      @change="searchData"
    ></el-date-picker>
    
    • 开始时间在当前日期之后
    pickerOptions: {
      disabledDate(time) {
      return time.getTime() < Date.now() - 8.64e7;
      }
    },  
    
    • 开始时间结束时间控制在一定范围内,一般是接口的返回数据 ->this.data
    this.pickerOptions = Object.assign(this.pickerOptions,{
      disabledDate: (time) => {
      return (time.getTime() < new Date(this.data.startTime)  - 8.64e7) || time.getTime() > new Date(this.data.endTime)
      },
    })
    
    

    中间的连接符一定要是||,return 有个布尔型默认值

    • 时间范围控制在一周之内
    `在data定义pickerMinDate变量 `
    let that = this;  
    this.pickerOptions = Object.assign(this.pickerOptions,{
        onPick: ({ maxDate, minDate }) => {
        that.pickerMinDate = minDate.getTime();
            if (maxDate) {
            that.pickerMinDate = '';
            }
          },
        disabledDate(time) {
        // onPick后触发
        if (that.pickerMinDate !== '') {
              //结束时间在开始时间7天内
            let one =  6 * 24 * 3600 * 1000,
             minTime = that.pickerMinDate - one,
             maxTime = that.pickerMinDate + one;
            if(minTime && maxTime ){
            return time.getTime() < minTime || time.getTime() > maxTime
            }
        }
        // return time.getTime() > Date.now();
      }
    })
    

    相关文章

      网友评论

          本文标题:el-date-picker

          本文链接:https://www.haomeiwen.com/subject/oolssctx.html