ElementUI 年份范围选择器 el-year-picker

效果图如下:

代码如下:

js 复制代码
<template>
  <div class="year-range-picker">
    <el-date-picker
      v-model="dateList[0]"
      type="year"
      placeholder="选择开始年"
      :size="size"
      class="year-picker"
      format="yyyy"
      :value-format="valueFormat"
      :picker-options="startDatePicker"
    >
    </el-date-picker>
    <span class="range-word"> 至 </span>
    <el-date-picker
      v-model="dateList[1]"
      type="year"
      placeholder="选择结束年"
      :size="size"
      class="year-picker"
      :value-format="valueFormat" 
      :picker-options="endDatePicker"
    >
    </el-date-picker>
  </div>
</template>


<script>
export default {
  name: "el-year-picker",
  // 接收父组件传入的数据
  props: {
    value: {
      // type: Array,
      required: true,
    },
    size: {
      type: String, default: 'mini'
    },
    valueFormat: {
      type: String, default: 'yyyy'
    },
  },
  data() {
    return {
      dateList: [],
      startDatePicker: this.beginDate(),
      endDatePicker: this.processDate(),
    };
  },
  watch: {
    value(v) {
      this.dateList = v || []
    },
    dateList(v) {
      this.$emit('input', v)
    }
  },
  mounted() {
  },
  methods: {
    // 选择年份范围选择时开始时间不能大于结束时间,结束时间不能小于开始时间

    // 提出开始时间必须小于提出结束时间
    beginDate() {
      let self = this
      return {
        disabledDate(time) {
          if (self.dateList[1] !== '') {
            let fixedTime = new Date(time)
            return fixedTime.getFullYear() > self.dateList[1]
          }
        }
      }
    },
    // 提出结束时间必须大于提出开始时间
    processDate() {
      let self = this
      return {
        disabledDate(time) {
          // let fixedTime = new Date(self.oldTime).getTime()
          // return time.getTime() < fixedTime
          let fixedTime = new Date(time)
          return fixedTime.getFullYear() < self.dateList[0]
        }
      }
    },

  },
};
</script>


<style scoped>
.year-range-picker {
  color: black;
  text-align: center;
  border: 1px solid #dcdfe6;
  border-radius:4px;
  line-height: 25px;
  overflow: hidden;
  display: flex;
  margin: 4px 0;
}

>>> .el-input__inner{
  border: 0;
  /*padding: 0;*/
  line-height: 28px;
  height: 28px;
  margin:0;
}

.range-word {
  margin-left: 10px;
  margin-right: 10px;
  font-size: 12px;
}

.year-range-picker .year-picker {
  max-width: 150px;
  margin:0;
}
</style>
相关推荐
xiaofeichaichai1 天前
Webpack
前端·webpack·node.js
问心无愧05131 天前
ctf show web入门111
android·前端·笔记
唐某人丶1 天前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界1 天前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌1 天前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel1 天前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3111 天前
https连接传输流程
前端·面试
徐小夕1 天前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab1 天前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器