Loading

dhtmlx-gantt甘特图的使用

1.安装

npm i dhtmlx-gantt

2.组件导入

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

3.dom数据

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

4.data数据


  // tasks 包含以下基础字段 id: 任务唯一标识
  // text: 任务名称
  // start_date: 任务开始时间
  // duration: 任务时长
  // progress: 任务进度
tasks: {
        data: [
          { "id": "1635835705701744641", "text": "345分项工程分项工程分项工程", "start_date": "2022-12-01", "beginTime": "2022-12-01", "endTime": "2023-03-14", "limitTime": 103, "duration": 103, "progress": 1 },
          { "id": "1635835705701744642", "text": "111", "start_date": "2023-03-14", "beginTime": "2023-03-14", "endTime": "2023-03-17", "limitTime": 3, "duration": 3, "progress": 1 },
          { "id": "1635835705701744643", "text": "分项工程", "start_date": "2023-03-13", "beginTime": "2023-03-13", "endTime": "2023-03-23", "limitTime": 10, "duration": 10, "progress": 1 },
          { "id": "1635835705701744644", "text": "分项工程1", "start_date": "2023-03-30", "beginTime": "2023-03-30", "endTime": "2023-04-20", "limitTime": 21, "duration": 21, "progress": 1 },
          { "id": "1635835705701744645", "text": "分项工程2", "start_date": "2023-03-21", "beginTime": "2023-03-21", "endTime": "2023-03-29", "limitTime": 8, "duration": 8, "progress": 1 },
          { "id": "1635835705701744646", "text": "分项工程3", "start_date": "2023-03-23", "beginTime": "2023-03-23", "endTime": "2023-03-31", "limitTime": 8, "duration": 8, "progress": 1 },
          { "id": "1635835705701744647", "text": "6", "start_date": "2023-03-29", "beginTime": "2023-03-29", "endTime": "2023-03-30", "limitTime": 1, "duration": 1, "progress": 1 },
          { "id": "1635835705701744648", "text": "1", "start_date": "2023-03-16", "beginTime": "2023-03-16", "endTime": "2023-03-17", "limitTime": 1, "duration": 1, "progress": 1 },
          { "id": "1635835705701744649", "text": "999", "start_date": "2023-08-31", "beginTime": "2023-08-31", "endTime": "2024-02-29", "limitTime": 182, "duration": 182, "progress": 1 }
        ],
      },
scalesOptions: [{ label: '', value: 'week' }, { label: '', value: 'month' }],
// 展示单位
scales: 'week',

 

5.js代码

 mounted () {
    // 清空缓存
    gantt.clearAll()
this.initGantt()
 },
  1 initGantt () {
  2       gantt.config.order_branch = false; //左边表格 列可以拖动排序
  3       gantt.config.show_progress = true; //取消右边表格进度条显示与拖动
  4       gantt.config.min_grid_column_width = 120; //设置左侧表格宽度
  5       gantt.config.show_links = false; // 禁用连线 
  6       gantt.config.drag_progress = false; //禁用工作进度拖拽
  7       gantt.config.duration_unit = 'day'; // 设置任务分段参数以及单位
  8       gantt.config.duration_step = 1;
       gantt.config.drag_resize = false; // 可以通过拖放来调整任务大小
         gantt.config.drag_move = false; // 可以通过拖放来移动任务
  9 
 10       gantt.i18n.setLocale("cn");//设置语音为中文
 11 
 12       //自定义左侧 头部列的内容(方式1)
 13       gantt.config.columns = [
 14         {
 15           name: 'text',
 16           label: '分项工程',
 17           tree: true,
 18           width: '*',
 19           align: 'left',
 20           template: function (obj) { return `<span class="gantt-name-box" title="${obj.text}">${obj.text}</span>` }
 21         },
 22         {
 23           name: 'beginTime',
 24           label: '开始日期',
 25           width: '100',
 26           align: 'center',
 27           template: function (obj) {
 28             return obj.beginTime;
 29           }
 30         },
 31         {
 32           name: 'endTime',
 33           label: '结束日期',
 34           width: '100',
 35           align: 'center',
 36           template: function (obj) {
 37             return obj.endTime;
 38           }
 39         },
 40         {
 41           name: 'limitTime',
 42           label: '工期',
 43           width: '100',
 44           align: 'center',
 45           template: function (obj) {
 46             return `${obj.limitTime}天`;
 47           }
 48         }
 49       ];
 50 
 51 
 52       gantt.config.xml_date = '%Y-%m-%d'; // 日期格式化的匹配格式
 53       gantt.config.scale_height = 90; // 日期栏的高度
 54 
 55       const scalesInfo = {
 56         week: [
 57           { unit: 'year', step: 1, format: '%Y' },
 58           { unit: 'week', step: 1, format: this.weekScaleTemplate },
 59         ],
 60         month: [
 61           { unit: 'year', step: 1, format: '%Y' },
 62           { unit: 'month', step: 1, format: '%M' },
 63         ],
 64       }
 65       gantt.config.scales = scalesInfo[this.scales];
 66 
 67       // 添加今日的Marker
 68       gantt.plugins({
 69         marker: true
 70       });
 71       gantt.addMarker({
 72         start_date: new Date(),
 73         text: '今日'
 74       });
 75       // 添加今日的Marker end
 76       //其他事件 (禁用原来自带的弹窗)
 77       gantt.attachEvent(
 78         'onBeforeLightbox',
 79         function (id) {
 80           console.log(1);
 81           return false; // 返回 false
 82         },
 83         {}
 84       );
 85       //禁用任务双击进入编辑事件
 86       gantt.attachEvent(
 87         'onTaskDblClick',
 88         function (id, e) {
 89           console.log('id', id, e);
 90           dialogVisible.value = true;
 91           return false;
 92         },
 93         {}
 94       );
 95       // 给年月展示框加类名
 96       // gantt.templates.scale_cell_class = () => 'month-box';
 97       gantt.init(this.$refs.gantt); //这里可以通过ref挂载 或者 id选择器挂载
 98       gantt.parse(this.tasks);
 99     },
100     weekScaleTemplate (date) {
101       // 可以时使用dayjs 处理返回
102       const dateToStr = gantt.date.date_to_str('%d');
103       const mToStr = gantt.date.date_to_str('%M');
104       const endDate = gantt.date.add(gantt.date.add(date, 1, 'week'), -1, 'day');
105       // 处理一下星期
106       return `${mToStr(endDate)}${dateToStr(endDate)}日`;
107     },

6.css代码

 1 <style lang="scss" scoped>
 2 .gantt-container {
 3   height: calc(100vh - 200px);
 4 }
 5 
 6 // 字数多时隐藏改为...
 7 ::v-deep .gantt-name-box {
 8   box-sizing: border-box;
 9   display: block;
10   width: 100%;
11   text-align: left;
12   overflow: hidden;
13   text-overflow: ellipsis;
14   white-space: nowrap;
15 }
16 </style>

 7.api文档地址 

https://docs.dhtmlx.com/gantt/api__refs__gantt.html

 

posted @ 2023-03-23 09:40  请叫我王小胖  阅读(3505)  评论(0编辑  收藏  举报