vue element 多个日期组件选择禁止重叠

bash 复制代码
<template>
  <div>
    <div v-for="(item, index) in timeList" :key="index">
      <el-date-picker
        v-model="item.time"
        type="daterange"
        :picker-options="pickerOption(index)"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button
        type="text"
        @click="delTime(index)"
      >删除</el-button>
    </div>
    <el-button
      type="text"
      @click="addTime"
    >+添加时间</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 用于存储所有日期选择器的选中值
      timeList: []
    }
  },
  methods: {
    addTime() {
      const ele = {
        time: []
      }
      this.timeList.push(ele)
      console.log('--', this.timeList)
    },
    delTime(index) {
      this.timeList.splice(index, 1)
    },
    pickerOption(index) {
      const option = {
        disabledDate: (time) => {
          // 过滤当前日期
          const times = this.timeList.map(it => it.time).filter((it, i) => it && i !== index)
          let timeScope = null
          times.forEach((item, indexs) => {
            if (indexs === 0) {
              // -1 解决开始日之前一天不可选中的问题
              timeScope = (time.getTime() > new Date(item[0]).getTime() - 60 * 60 * 24 * 1000 &&
                time.getTime() < new Date(item[1]).getTime() + 1)
            } else {
              timeScope = timeScope || (time.getTime() > new Date(item[0]).getTime() - 60 * 60 * 24 * 1000 &&
                time.getTime() < new Date(item[1]).getTime() + 1)
            }
          })
          return timeScope || (time.getTime() > new Date('2023-12-30').getTime() - 60 * 60 * 24 * 1000 || time.getTime() < new Date('2023-12-01').getTime())
        }
      }
      return option
    }
  }
}
</script>
相关推荐
小高0071 小时前
🌐ES6 这 8 个隐藏外挂,知道 3 个算我输!
前端·javascript·面试
东坡白菜1 小时前
SSE 实现 AI 对话中的流式输出
javascript·vue.js
小桥风满袖2 小时前
极简三分钟ES6 - ES9中字符串扩展
前端·javascript
前端人类学2 小时前
掌控异步洪流:多请求并发下的顺序控制艺术
javascript·promise
CryptoRzz3 小时前
印度尼西亚股票数据API对接实现
javascript·后端
lecepin4 小时前
AI Coding 资讯 2025-09-17
前端·javascript·面试
猩兵哥哥4 小时前
前端面向对象设计原则运用 - 策略模式
前端·javascript·vue.js
江城开朗的豌豆5 小时前
解密React虚拟DOM:我的高效渲染秘诀 🚀
前端·javascript·react.js
江城开朗的豌豆5 小时前
React应用优化指南:让我的项目性能“起飞”✨
前端·javascript·react.js
Asort5 小时前
JavaScript 从零开始(六):控制流语句详解——让代码拥有决策与重复能力
前端·javascript