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>
相关推荐
2601_949194267 分钟前
TypeScript 与后端开发Node.js
javascript·typescript·node.js
前端那点事14 分钟前
Vue3 defineModel 完全不破坏单向数据流!底层原理+实战解析
vue.js
hmh1234519 分钟前
录音与音频可视化
前端·javascript
阿丰资源1 小时前
Java项目基于SpringBoot+Vue前后端分离在线商城系统(附源码)
java·vue.js·spring boot
江-月*夜1 小时前
vue3 wordcloud2.js词云使用
开发语言·javascript·vue.js
吴声子夜歌1 小时前
Vue3——Vuex状态管理
前端·vue.js·vue·es6
qq_12084093711 小时前
Three.js 工程向:Frustum Culling 与场景分块优化实战
前端·javascript
ZC跨境爬虫1 小时前
3D地球卫星轨道可视化平台开发 Day14(彻底移除多余阴影)
前端·javascript·3d·信息可视化·json
不会敲代码12 小时前
从零开始读懂 MCP:大模型如何通过标准化协议“调用”你的工具?
javascript·cursor·trae