a-datePricker组件多区间禁用

在许多子表的应用场景下 会产生时间选择不允许出现重叠的情况,可以使用如下方法
目前应用场景

html代码如下

<a-table
          :columns="columns"
          bordered
          :data-source="Datasource"
          size="small"
          class="components-table-demo-nested"
        >
          <template #bodyCell="{ column, record, index }">
                <div class="item" v-if="column.dataIndex == 'startDate'">
                  <a-date-picker
                    v-model:value="record.startDate"
                    :valueFormat="'YYYY-MM-DD'"
                    :disabledDate="disabledStartDate"
                  ></a-date-picker>
                </div>
                <div class="item" v-if="column.dataIndex == 'endDate'">
                  <a-date-picker
                    v-model:value="record.endDate"
                    :valueFormat="'YYYY-MM-DD'"
                    :disabledDate="(current) => disabledEndDate(current, record)"
                  ></a-date-picker>
                </div>
                <template/>
         <a-table/>

js 代码

<script setup>
import {ref} from "vue"
  const columns = ref([
    { title: '开始时间', dataIndex: 'startDate', key: 'startDate', align: 'center', width: '25%' },
    { title: '结束时间', dataIndex: 'endDate', key: 'endDate', align: 'center', width: '25%' },
  ]);
  const Datasource = ref([])
  // 时间对比
  // current 时间戳 , disabledDateList 数组数据, rowsStartDate 行开始时间 结束时间禁用
  const disDate = (current, disabledDateList, rowsStartDate = null) => {
    let type = false;
    let date = new Date(current);
    let c = new Date(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`).getTime();
    for (let i = 0; i < disabledDateList.length; i++) {
      // 新增的空数据过滤 减少资源损耗
      if (!disabledDateList[i].startDate && !disabledDateList[i].endDate) {
        break;
      }
      let startDate = new Date(disabledDateList[i].startDate).getTime() - 28800000;
      let endDate = new Date(disabledDateList[i].endDate).getTime();
      let recordStartDate = rowsStartDate ? new Date(rowsStartDate).getTime() : '';
      if (recordStartDate) {
        // 禁用所有小于行开始的时间   禁用所有 开始时间大于 行时间 的数据
        if (c <= recordStartDate || (startDate > recordStartDate && c > startDate)) {
          type = true;
          break;
        }
      } else {
        // 禁用 多个时间段
        if (c <= endDate && c >= startDate) {
          type = true;
          break;
        }
      }
    }
    return type;
  };
  // 开始时间禁用逻辑
  const disabledStartDate = (current) => {
    let date = new Date(current).getTime();
    let tarDate = new Date().getTime();
    return Datasource.value.length == 1
      ? current && date < tarDate
      : (current && date < tarDate) || disDate(current, Datasource.value);
  };
  // 结束时间禁用逻辑
  const disabledEndDate = (current, record) => {
    let date = new Date(current).getTime();
    return (
      current &&
      (record.startDate
        ? date < new Date(record.startDate).getTime() ||
          disDate(current, Datasource.value, record.startDate)
        : true)
    );
  };
<script/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗_都不做前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值