4
头图

之前看到有人需要一个年份范围选择器,由于element-ui里没有提供<el-date-picker type="yearange">的相似功能,故自己写了一个简单的《年份范围选择器》。
下面是组件代码,其中有用到moment这个js日期时间库,不需要的可以使用new Date()进行处理。

<template>
  <div class="picker-year">
    <el-form ref="year-range-picker" :model="formData" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item prop="yearStart">
            <el-date-picker
              class="picker-first"
              v-model="formData.yearStart"
              value-format="yyyy"
              @change="val=>onDateChange(0,val)"
              :picker-options="pickerOptionsStart"
              :placeholder="startPlaceholder"
              type="year">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="yearEnd">
            <el-date-picker
              class="picker-second"
              v-model="formData.yearEnd"
              value-format="yyyy"
              @change="val=>onDateChange(1,val)"
              :picker-options="pickerOptionsEnd"
              :placeholder="endPlaceholder"
              type="year">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'

export default {
  name: "YearRangePicker",
  data() {
    return {
      formData: {
        yearStart: '',
        yearEnd: '',
      },
      rules: {
        yearStart: [
          {required: true, message: '请选择开始年份', trigger: 'change'}
        ],
        yearEnd: [
          {required: true, message: '请选择结束年份', trigger: 'change'}
        ],
      },
      pickerOptionsStart: {
        disabledDate(date) {
          return false
        },
      },
      pickerOptionsEnd: {
        disabledDate(date) {
          return false
        },
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldValue) {
        if (newVal) {
          this.yearStart = newVal[0]
          this.yearEnd = newVal[1]
        }
      },
      immediate: true
    },
  },
  props: {
    value: {
      type: Array,
      default: []
    },
    startPlaceholder: {
      type: String,
      default: ''
    },
    endPlaceholder: {
      type: String,
      default: ''
    }
  },
  methods: {
    onDateChange(index, value) {
      this.pickerOptionsStart = {
        disabledDate: (date) => {
          return (moment(date) > moment().year(this.yearEnd))
        },
      }
      this.pickerOptionsEnd = {
        disabledDate: (date) => {
          return moment(date) <= moment().year(this.yearStart)
        },
      }
      if (!value) {
        this.rules = {
          yearStart: [
            {required: false, message: '请选择开始年份', trigger: 'change'}
          ],
          yearEnd: [
            {required: false, message: '请选择结束年份', trigger: 'change'}
          ],
        }
        this.formData = {
          yearStart: '',
          yearEnd: ''
        }
      } else {
        this.rules = {
          yearStart: [
            {required: true, message: '请选择开始年份', trigger: 'change'}
          ],
          yearEnd: [
            {required: true, message: '请选择结束年份', trigger: 'change'}
          ],
        }
      }
      this.$refs["year-range-picker"].validate((valid) => {
        if (valid) {
          console.log('校验结果', index, value, [this.formData.yearStart, this.formData.yearEnd])
          this.$emit('change', [this.formData.yearStart, this.formData.yearEnd])
          this.$emit('input', [this.formData.yearStart, this.formData.yearEnd])
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
}
</script>

<style scoped>
.picker-year {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
}

.picker-first {
  width: 100%;
}

.line {
  text-align: center;
}

.picker-second {
  width: 100%;
}
</style>

以下是在页面中调用的DEMO。

<year-picker
        v-model="dataQuery.yearRange" 
        start-placeholder="开始年份" 
        end-placeholder="结束年份" 
        @change="val=>console.log(val)"/>
【Tip】yearRange的数据类型格式为[String,String],如["2012","2022"]

泉州牧码人
73 声望4 粉丝

前端工程师