Vue2 Element-UI Datepicker force refresh for dynamic disabledDate

五迷三道 提交于 2019-12-11 04:38:13

问题


In the codepen below I have a Element-UI datepicker set up to show a dynamic disabled dates based on a random number.

The number of disabled dates change every time the datepicker input comes into focus.

My issue is the datepicker doesn't refresh the disabled dates until you click on a different month. The datepicker also shows the last month you were previously on when when you click off and back in.

Is there a way to force Element-UI Datepicker to refresh? I would like to make the datepicker refresh in the on focus event after the new disabled value is set.

https://codepen.io/anon/pen/rbXjLr

Element-UI Datepicker Documentation

<div id="app">
<template>
  <div class="block">
    <span class="demonstration">Picker with quick options</span>
    <el-date-picker           
      v-model="value2"
      type="date"             
      placeholder="Enter Date"
      @focus="focus()"
      :default-value="defaultValue"
      :picker-options="pickerOptions">
    </el-date-picker>
  </div>
</template>
</div>

var is10Days = "";
var Randomizer = (function(){
  var is10DaysSetter = function(){
    if(is10Days === "") {
      is10Days = Math.round(Math.random()) === 1;        
    }
    //console.log("is10Days: " + is10Days);
    return is10Days;
  }
  return {
    Is10DaysSetter: is10DaysSetter
  }
})()

var Main = {
    data() {
      return {
        defaultValue: "",
        pickerOptions: {
          disabledDate(time) {
            var self = this;
            var date = moment()._d;
            var mindate = moment().subtract(5,'d')._d;
            var maxDate = moment()._d;         
            var isBeforeMinDate = time.getTime() < mindate;
            var isAfterMaxDate =  time.getTime() > maxDate; 

            if(is10Days !== "" && is10Days){
              var alternateMinDate = date.setDate(date.getDate() - 10);
               isBeforeMinDate = time.getTime() < alternateMinDate;
            }
            //console.log("disabledDate");
            return isBeforeMinDate || isAfterMaxDate;
          }
        },
        value2: '',
      };
    },
    methods:{
      focus: function() {
        var self = this;
        is10Days = "";
        self.defaultValue = moment().format("YYYY-MM-DD");
        Randomizer.Is10DaysSetter();
        console.log("reset is10Days: " + (is10Days ? "10 days" : "5 days"));
      }
    }
  };
var Ctor = Vue.extend(Main)
ELEMENT.locale(ELEMENT.lang.en)

new Ctor().$mount('#app')

回答1:


I posted this as a feature request on Element UI's git hub and received a response:

https://github.com/ElemeFE/element/issues/15380

<el-date-picker 

  ref="picker" //Added this

  v-model="value2"
  type="date"             
  placeholder="Enter Date"
  @focus="focus()"
  :picker-options="pickerOptions">
</el-date-picker>

    methods:{
  focus: function() {
    var self = this;

    is10Days = "";
    Randomizer.Is10DaysSetter();

    //Added this
    this.$nextTick(_ => {
      this.$refs.picker.picker.date = new Date()
    })

    console.log("reset is10Days: " + (is10Days ? "10 days" : "5 days"));
  }
}

Adding a reference to picker allowed me to override the unwanted feature of going back to the previously viewed month and solved my issue. This came with a warning that since this is not part of the public API, it could change in a future version.

Here is a link to a working code pen:

https://codepen.io/steveshore/pen/rbXjLr




回答2:


I think it is about compomemt render. when your main vue app initialize, <el-date-picker> rendered completly first.

The problem is when date-picker finished rendering, are the main vue datas ready?

It seems you pass a null into options, but select another month will force update options.

could you try this? How to Initialize Data Properties with Prop Values make a v-if="pickerOptions" in attr



来源:https://stackoverflow.com/questions/55975079/vue2-element-ui-datepicker-force-refresh-for-dynamic-disableddate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!