在 Vue 3 中使用 DHTMLX 甘特图组件

简介: 本文将介绍如何在 Vue 3 项目中集成 DHTMLX 甘特图组件,详细讲解安装、模块导入以及基本用法。通过示例代码,您将学会如何配置甘特图的任务、样式和交互功能,帮助您在项目中更有效地管理和展示任务时间线。

安装

npm install dhtmlx-gantt

模块导入

import gantt from "dhtmlx-gantt"; // 引入模块
import "dhtmlx-gantt/codebase/dhtmlxgantt.css"; // 引入甘特图样式

使用示例

<template>
  <div class="gantt-container">
    <div ref="gantt" class="gantt-content"></div>
  </div>
</template>

<script>
import gantt from 'dhtmlx-gantt';
import 'dhtmlx-gantt/codebase/dhtmlxgantt.css';
import { parseTime } from '@/utils'; // 自定义时间格式化方法

export default {
  name: 'GanttTask',
  data() {
    return {
      tasks: {
        data: [
          { id: 11, text: '任务1', start_date: '18-04-2018', end_date: '19-04-2018', progress: 1 },
          { id: 12, text: '任务2', start_date: '19-04-2018', end_date: '22-04-2018', progress: 0.6 },
          // 更多任务...
        ]
      }
    };
  },
  mounted() {
    this.initGantt();
  },
  methods: {
    initGantt() {
      gantt.config.fit_tasks = true;
      gantt.config.drag_project = true;
      gantt.config.scale_height = 80;
      gantt.config.row_height = 60;
      gantt.config.bar_height = 40;
      gantt.i18n.setLocale('cn');
      gantt.config.autosize = true;
      gantt.config.readonly = true;
      gantt.config.show_grid = true;
      gantt.plugins({ tooltip: true });
      gantt.templates.tooltip_text = (start, end, task) => `
        <div>
          <div>任务:${task.text}</div>
          <div>开始时间:${parseTime(task.start_date, '{y}-{m}-{d}')}</div>
          <div>结束时间:${parseTime(task.end_date, '{y}-{m}-{d}')}</div>
          <div>进度:${task.progress * 100}%</div>
        </div>`;
      gantt.config.scales = [
        { unit: 'month', step: 1, date: '%F, %Y' },
        { unit: 'day', step: 1, date: '%j, %D' }
      ];
      gantt.config.columns = [
        { name: 'text', label: '任务内容', width: '120', align: 'center' }
      ];
      gantt.init(this.$refs.gantt);
      gantt.parse(this.tasks);
    }
  }
};
</script>

<style lang="scss">
.gantt_task {
  background: #79bbff;
}
.gantt_task_progress {
  background: #53a8ff;
}
.gantt_grid_data .gantt_row.gantt_selected {
  background: #f0f3f9;
}
</style>

效果图

甘特图示例

目录
相关文章
|
3月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
311 2
|
2月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
505 139
|
2月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
218 1
|
6月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
787 0
|
3月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
381 11
|
2月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
262 0
|
6月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
4月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
439 1
|
4月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
254 0
|
5月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
136 0