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>
相关推荐
浩星4 分钟前
vue3+uniapp中使用高德地图实现撒点效果
前端·vue.js·uni-app
JamSlade10 分钟前
React 个人笔记 Hooks编程
前端·javascript·笔记·react.js
程序二次开发19 分钟前
html,js获取扫码设备的输入内容
前端·javascript·html
F2E_Zhangmo21 分钟前
webpack5所用依赖以及对应的版本
vue.js·webpack
鲁Q同志1 小时前
vue项目启动报错(node版本与Webpack)
前端·vue.js·webpack
Elastic 中国社区官方博客1 小时前
在 JavaScript 中正确使用 Elasticsearch,第二部分
大数据·javascript·数据库·elasticsearch·搜索引擎·全文检索
Favour722 小时前
根据当前日期计算并选取上一个月和上一个季度的日期范围,用于日期控件的快捷选取功能
前端·vue.js·elementui
初遇你时动了情3 小时前
flutter长列表 ListView、GridView、SingleChildScrollView、CustomScrollView区别
前端·javascript·flutter
maozexijr3 小时前
Flink的时间问题
javascript·算法·flink
我是大头鸟4 小时前
SpringMVC 通过ajax 实现文件的上传
前端·javascript·ajax