当前位置:首页 > VUE

vue 实现拖拽甘特图

2026-02-11 03:14:56VUE

实现拖拽甘特图的核心步骤

使用 Vue 实现拖拽甘特图需要结合第三方库或自定义逻辑,核心功能包括任务条渲染、时间轴刻度、拖拽交互等。以下是具体实现方法:

选择基础库

推荐使用 vue-draggablevuedraggable 处理拖拽逻辑,配合 SVG 或 Canvas 渲染甘特图。若需完整功能,可直接集成开源库如 dhtmlx-ganttvue-gantt-elastic

安装基础依赖:

npm install vuedraggable @svgdotjs/svg.js

数据结构设计

甘特图任务数据通常为嵌套结构,包含任务名称、开始/结束时间、层级关系等:

tasks: [
  {
    id: 1,
    name: "任务A",
    start: "2023-01-01",
    end: "2023-01-05",
    progress: 80,
    children: [
      { id: 2, name: "子任务A1", start: "2023-01-01", end: "2023-01-03" }
    ]
  }
]

时间轴渲染

使用 SVG 或 CSS Grid 绘制水平时间轴,动态计算刻度位置:

vue 实现拖拽甘特图

<div class="gantt-header">
  <div 
    v-for="day in timeRange" 
    :key="day.date" 
    :style="{ width: `${dayWidth}px` }"
  >
    {{ day.label }}
  </div>
</div>

计算时间范围的逻辑:

computed: {
  timeRange() {
    const range = [];
    let current = new Date(this.startDate);
    while (current <= new Date(this.endDate)) {
      range.push({
        date: current.toISOString(),
        label: current.getDate()
      });
      current.setDate(current.getDate() + 1);
    }
    return range;
  }
}

任务条拖拽实现

通过 vuedraggable 实现任务条的拖拽和位置更新:

<draggable 
  v-model="tasks"
  group="gantt"
  @end="onDragEnd"
>
  <div 
    v-for="task in tasks" 
    :key="task.id"
    class="gantt-task"
    :style="{
      left: `${getTaskPosition(task.start)}px`,
      width: `${getTaskDuration(task.start, task.end)}px`
    }"
  >
    {{ task.name }}
  </div>
</draggable>

拖拽结束后的回调处理:

vue 实现拖拽甘特图

methods: {
  onDragEnd(evt) {
    const movedTask = this.tasks[evt.newIndex];
    const newStart = this.calculateDateFromPosition(evt.item.style.left);
    // 更新任务时间
    movedTask.start = newStart;
    movedTask.end = this.calculateEndDate(newStart, movedTask.duration);
  }
}

动态调整任务时长

添加拖拽手柄允许调整任务条长度:

<div class="gantt-task">
  <div class="task-content">{{ task.name }}</div>
  <div 
    class="resize-handle"
    @mousedown="startResize(task)"
  ></div>
</div>

调整逻辑示例:

startResize(task, evt) {
  window.addEventListener('mousemove', this.handleResize);
  window.addEventListener('mouseup', this.stopResize);
  this.resizingTask = task;
},

handleResize(evt) {
  if (!this.resizingTask) return;
  const newWidth = evt.clientX - this.$el.getBoundingClientRect().left;
  this.resizingTask.end = this.calculateDateFromPosition(newWidth);
}

进阶优化方向

  1. 性能优化:虚拟滚动处理大量任务
  2. 依赖关系:使用箭头连接关联任务
  3. 缩放控制:添加时间单位切换(天/周/月)
  4. 状态持久化:保存拖拽后的任务数据到后端

完整实现可参考开源项目:

标签: 拖拽甘特图
分享给朋友:

相关文章

甘特图vue实现

甘特图vue实现

甘特图在 Vue 中的实现方法 使用现成库(推荐) 推荐使用 vue-ganttastic 或 dhtmlx-gantt 等现成库快速实现甘特图功能。 安装 vue-ganttastic: npm…

vue拖拽容器实现

vue拖拽容器实现

Vue 拖拽容器实现方法 在 Vue 中实现拖拽容器功能可以通过原生 HTML5 的拖放 API 或第三方库如 vuedraggable 来实现。以下是两种方法的详细说明。 使用 HTML5 拖放…

vue实现carousel拖拽

vue实现carousel拖拽

实现拖拽轮播的基本思路 在Vue中实现可拖拽的Carousel组件需要结合触摸事件(touchstart、touchmove、touchend)和鼠标事件(mousedown、mousemove、mo…

vue实现拖拽连线

vue实现拖拽连线

Vue 实现拖拽连线的方法 使用第三方库(推荐) 对于快速实现拖拽连线功能,推荐使用成熟的第三方库如 jsplumb 或 vue-draggable。这些库提供了丰富的 API 和事件支持,能够简化开…

前端vue实现拖拽

前端vue实现拖拽

Vue 实现拖拽功能的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现拖拽表格

vue实现拖拽表格

Vue 实现拖拽表格的方法 使用第三方库(推荐) 推荐使用 vuedraggable 或 sortablejs 这类成熟的拖拽库,它们对 Vue 有良好支持且功能丰富。 安装 vuedraggab…