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>
相关推荐
Aphasia31116 分钟前
面试八股文——vue篇📑
前端·vue.js·面试
A尘埃21 分钟前
前端ES6基本语法,以及前端项目模板vue-admin-template和后端进行对接(跨域问题的解决)
前端·vue.js·es6·前后端对接
Dignity_呱23 分钟前
小小导出,我大前端一人足矣(复杂多级表头)
前端·vue.js·面试
大叔_爱编程35 分钟前
wx219基于ssm+vue+uniapp的教师管理系统小程序
vue.js·小程序·uni-app·毕业设计·ssm·源码·课程设计
ak啊37 分钟前
Vue3 + Vite4 项目的性能优化配置方案
前端·vue.js·vite
Mintopia41 分钟前
Node.js 对前端技术有利的知识点
前端·javascript·node.js
一道雷43 分钟前
🛠️ Unindex:推荐一款自动化索引文件生成工具
前端·javascript·node.js
键指江湖1 小时前
React 更新 state 中的数组
javascript·react.js·ecmascript
Mintopia1 小时前
Three.js 着色器使用教程:进阶指南
前端·javascript·three.js
冰镇生鲜1 小时前
《v-model原理 》以及 《自定义组件实现v-model》
前端·vue.js