element 月份选择器 范围选择限制可选区间
如上图:在月份选择器里只能选2月到8月之间
代码如下:
<el-date-picker
v-model="ruleForm.postMonth"
type="monthrange"
value-format="yyyy-MM"
range-separator="至"
start-placeholder="开始月份"
end-placeholder="结束月份"
clearable
:picker-options="pickerOptions"
>
</el-date-picker>
data () {
return {
pickerOptions: {
onPick: this.onPick,
disabledDate: this.disabledDate
},
time: ''
}
},
methods: {
disabledDate (e) {
let minMonth = '202302'
let maxMonth = '202308'
// 时间选择器月份信息
const timeyear = e.getFullYear()
let timemonth = e.getMonth() + 1
if (timemonth >= 1 && timemonth <= 9) {
timemonth = '0' + timemonth
}
const elTimeData = timeyear.toString() + timemonth.toString()
if (elTimeData > maxMonth) {
return true
}
if (elTimeData < minMonth) {
return true
}
},
onPick ({ maxDate, minDate }) {
if (!maxDate) {
this.time = minDate
} else {
this.time = ''
}
}
}