当前位置:首页 > VUE

vue 实现拖拽甘特图

2026-03-28 13:22:48VUE

实现拖拽甘特图的基本思路

在Vue中实现拖拽甘特图需要结合第三方库或手动实现拖拽逻辑,核心功能包括任务条渲染、时间轴刻度、拖拽交互等。以下是基于vue-ganttastic和手动实现的两种方案。

使用vue-ganttastic库快速实现

vue-ganttastic是一个轻量级甘特图组件库,支持拖拽任务条和调整时间范围。

安装依赖:

vue 实现拖拽甘特图

npm install vue-ganttastic

基础示例代码:

<template>
  <GanttChart :tasks="tasks" @task-updated="handleTaskUpdate" />
</template>

<script>
import { GanttChart } from 'vue-ganttastic';

export default {
  components: { GanttChart },
  data() {
    return {
      tasks: [
        { id: 1, name: 'Task 1', start: '2023-01-01', end: '2023-01-05' },
        { id: 2, name: 'Task 2', start: '2023-01-06', end: '2023-01-10' }
      ]
    };
  },
  methods: {
    handleTaskUpdate(updatedTask) {
      const index = this.tasks.findIndex(t => t.id === updatedTask.id);
      this.tasks.splice(index, 1, updatedTask);
    }
  }
};
</script>

手动实现拖拽甘特图

若需高度自定义,可通过以下步骤实现:

vue 实现拖拽甘特图

1. 任务条渲染

使用v-for渲染任务条,绑定样式计算位置:

<template>
  <div class="gantt-container">
    <div 
      v-for="task in tasks" 
      :key="task.id"
      class="task-bar"
      :style="{
        left: calculateLeft(task.start),
        width: calculateWidth(task.start, task.end)
      }"
      draggable="true"
      @dragstart="dragStart(task.id)"
      @dragend="dragEnd"
    ></div>
  </div>
</template>

2. 拖拽逻辑

记录拖拽起始位置并更新任务数据:

methods: {
  dragStart(taskId) {
    this.draggedTaskId = taskId;
  },
  dragEnd(e) {
    const newStartDate = this.calculateDateFromPosition(e.clientX);
    const task = this.tasks.find(t => t.id === this.draggedTaskId);
    task.start = newStartDate;
  },
  calculateDateFromPosition(x) {
    // 根据鼠标位置计算对应日期
  }
}

3. 时间轴计算

转换日期为像素位置:

calculateLeft(startDate) {
  const start = new Date(this.timeRange.start);
  const current = new Date(startDate);
  const diffDays = Math.floor((current - start) / (1000 * 60 * 60 * 24));
  return `${diffDays * this.dayWidth}px`;
}

关键细节优化

  • 时间刻度渲染:使用<div class="time-scale">横向平铺日期标签
  • 拖拽限制:通过@dragover.prevent防止默认行为,检查边界条件
  • 性能优化:对大型数据集使用虚拟滚动(如vue-virtual-scroller

完整实现可能需要结合resize监听、时间单位切换(日/周/月)等功能,复杂场景建议选择成熟库如dhtmlxGanttfrappe-gantt

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

相关文章

js实现图片拖拽

js实现图片拖拽

实现图片拖拽的基本步骤 HTML 结构需要包含可拖拽的图片元素,并设置 draggable 属性为 true: <img id="dragImage" src="image.jpg" drag…

vue 实现拖拽插件

vue 实现拖拽插件

Vue 拖拽插件实现方法 使用原生 HTML5 拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。在 Vue 中可以直接使用这些 API。…

vue实现图表拖拽

vue实现图表拖拽

Vue 实现图表拖拽功能 在 Vue 中实现图表拖拽功能,可以通过结合第三方图表库(如 ECharts、Chart.js)和拖拽库(如 interact.js、draggable)来完成。以下是几种常…

vue实现卡片拖拽

vue实现卡片拖拽

Vue 实现卡片拖拽的方法 使用 HTML5 拖放 API HTML5 提供了原生拖放 API,结合 Vue 可以轻松实现拖拽功能。以下是一个基本实现示例: <template> &…

用vue实现拖拽

用vue实现拖拽

使用 Vue 实现拖拽功能 Vue 可以通过原生 HTML5 的拖拽 API 或第三方库(如 vuedraggable)实现拖拽功能。以下是两种方法的详细实现方式。 使用 HTML5 拖拽 API…

vue实现控件拖拽

vue实现控件拖拽

Vue 实现控件拖拽的方法 在 Vue 中实现控件拖拽功能可以通过原生 HTML5 的拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种常见的方法: 使用 HTML5 拖放…