You need to enable JavaScript to run this app. 如何在FullCalendar中获取扩展事件详情及选中slot的详细信息
优惠活动
大模型
产品
解决方案
定价
更多

如何在FullCalendar中获取扩展事件详情及选中slot的详细信息

FullCalendar 点选时间Slot的信息获取与事件存在性判定方案

实现核心

通过FullCalendar官方提供的回调接口,可直接获取点选slot的精确信息,配合实例API即可完成slot范围内的事件存在性判定,无需手动监听DOM计算slot位置。

前置配置

需要先打开两个基础开关,配合现有slot粒度配置实现截图中的半透明高亮选中效果:

  • 开启selectable: true,允许选中slot区域
  • 开启selectMirror: true,渲染半透明的高亮选中层
  • 保持现有slotDuration(15/30分钟)配置不变,系统会自动将时间对齐到slot边界

完整实现代码

const calendar = new FullCalendar.Calendar(calendarDomEl, {
  initialView: 'timeGridWeek',
  slotDuration: '00:15:00', // 按实际业务调整为15/30分钟粒度
  selectable: true,
  selectMirror: true,
  // 高亮选中slot时触发
  select: (selectedInfo) => {
    // 直接拿到选中slot的完整时间范围
    const slotStart = selectedInfo.start;
    const slotEnd = selectedInfo.end;

    // 遍历所有已加载的事件,判定当前slot是否存在关联事件
    const allEvents = calendar.getEvents();
    const hasExistingEvent = allEvents.some(event => {
      // 重叠判定:只要事件和当前slot有时间交集,即判定存在关联
      return event.start < slotEnd && event.end > slotStart;
    });

    if (hasExistingEvent) {
      // 存在事件时的处理逻辑:弹出提示、取消选中
      calendar.unselect();
      return;
    }
    // 无事件时,携带slot时间打开新增事件表单
  },
  // 直接单击单个slot时触发
  dateClick: (clickInfo) => {
    // 可获取slot的ISO时间、所在DOM节点等信息
    console.log('单击slot的开始时间:', clickInfo.dateStr);
  }
});

注意要点

  • 回调返回的slot时间自动对齐配置的slot边界,哪怕点击在slot区间中间,也不会出现时间偏移,无需额外做取整计算
  • 事件存在性判定要采用时间重叠逻辑,不要硬卡事件时间与slot完全相等的条件,兼容跨多slot的长事件场景
  • 上述写法兼容FullCalendar v5、v6版本,全部基于官方公开API实现,不会和日历自身的交互逻辑产生冲突

内容的提问来源于stack exchange,提问作者Zonaib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 03:06:01