效果图:
需求如下:
1、只存在年份区间选择,不需要月和日期
2、初始年份定位到可选年份区间,而不是系统默认时间
<a-form-model-item label="年份" class="ele-input">
<el-date-picker
v-model="curStartYear"
type="year"
:picker-options="startPickerOptions"
format="yyyy"
value-format="yyyy"
@change="handleChangeStart"
:default-value="defaultValue"
>
</el-date-picker>
~
<el-date-picker
v-model="curEndYear"
type="year"
:picker-options="endPickerOptions"
format="yyyy"
value-format="yyyy"
@change="handleChangeEnd"
:default-value="defaultValue"
>
</el-date-picker>
</a-form-model-item>
定义初始值,修改:picker-options属性
data() {
return {
curStartYear: "",
curEndYear: "",
startPickerOptions: {},
endPickerOptions: {},
defaultValue: '',
}
},
//调用接口,将接口传回除外的年份置灰
methods:{
//限制年份可选范围
getStartPickerOptions() {
let result = this.years.map(v => {
return v.label
});
this.minYearStart = Math.min(...result)
this.minYearEnd = Math.max(...result)
//定位初始年份在可选年份区间,而不是系统默认年份
this.defaultValue = new Date();
this.defaultValue.setFullYear(Math.min(...result))
console.log('min', this.minYearStart)
console.log('max', this.minYearEnd)
this.startPickerOptions = Object.assign({}, this.startPickerOptions, {
disabledDate: (time) => {
return time.getFullYear() < this.minYearStart || time.getFullYear() > this.minYearEnd
}
});
this.endPickerOptions = Object.assign({}, this.endPickerOptions, {
disabledDate: (time) => {
return time.getFullYear() < this.minYearStart || time.getFullYear() > this.minYearEnd
}
});
},
}