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>
相关推荐
Amumu1213817 分钟前
Js:正则表达式(二)
开发语言·javascript·正则表达式
Sgf22738 分钟前
ES8(ES2017)新特性完整指南
开发语言·javascript·ecmascript
Cobyte3 小时前
1.基于依赖追踪和触发的响应式系统的本质
前端·javascript·vue.js
老神在在0014 小时前
【Selenium 自动化精讲】浏览器弹窗与登录界面的本质区别 & 实操指南
javascript·学习·selenium·测试工具·自动化
计算机学姐4 小时前
基于SpringBoot的咖啡店管理系统【个性化推荐+数据可视化统计+配送信息】
java·vue.js·spring boot·后端·mysql·信息可视化·tomcat
前端小咸鱼一条4 小时前
16.迭代器 和 生成器
开发语言·前端·javascript
web守墓人5 小时前
【前端】记一次将ruoyi vue3 element-plus迁移到arco design vue的经历
前端·vue.js·arco design
前端小阳5 小时前
JavaScript原型链
javascript
早點睡3905 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-collapsible
javascript·react native·react.js
前端Hardy5 小时前
别再手写代码了!2026 前端 5 个 AI 杀招,直接解放 80% 重复劳动(附工具+步骤)
前端·javascript·面试