当前位置:   article > 正文

在vue中element ui 结合frappe-gantt实现一个简单的甘特图功能_element ui 甘特图

element ui 甘特图
  1. 在vue中创建甘特图步骤请参考: https://editor.csdn.net/md/?articleId=130145782

2. 结合element ui 实现甘特图功能

实现效果:
在这里插入图片描述

2.1 下载element ui

因为我是在vue3中,所以下载element-plus 执行 npm i element-plus --save
main.js 里引入element ui

import { createApp } from 'vue'

import ElementPlus from 'element-plus';//1.引入组件
import 'element-plus/theme-chalk/index.css';//2.引入CSS
import locale from 'element-plus/lib/locale/lang/zh-cn';
import App from './App.vue';

createApp(App).use(ElementPlus, { locale }).mount('#app')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.2. 创建Gantt.vue组件

<template>
  <div class="gantt-container">
    <el-row>
      <el-col :span="8">
        <el-table
          :data="tasks"
          stripe
          style="width: 100%"
          row-key="id"
          border
          lazy
          :load="load"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="name" width="160" label="任务名称" show-overflow-tooltip />
          <el-table-column prop="start" width="140" label="开始日期" show-overflow-tooltip />
          <el-table-column prop="end" width="140" label="结束日期" show-overflow-tooltip />
          <el-table-column prop="date" width="140" label="持续时间" show-overflow-tooltip />
          <el-table-column prop="task" width="80" label="完成" show-overflow-tooltip />
          <el-table-column label="Operations" width="300">
            <template #default="scope">
              <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button
                size="small"
                type="danger"
                @click="handleAddChild(scope.$index, scope.row)"
              >添加子任务</el-button>
              <el-button
                v-if="scope.$index !== 0"
                size="small"
                type="danger"
                @click="handlemove(scope.$index, scope.row)"
              >上移一行</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="16">
        <div class="gantt-target"></div>
      </el-col>
    </el-row>
    <el-button type="primary" @click="handleADD">新增</el-button>
  </div>
</template>

<script>
import { reactive, toRefs, onMounted } from "vue";
import Gantt from "../assets/dist/frappe-gantt";
export default {
  setup() {
    const vueConfig = reactive({
      tasks: [ // 表格数据
        {
          start: "2023-04-01",
          end: "2023-04-08",
          name: "测试任务1",
          id: "1",
          progress: 26,
          task: "50%",
          date: 3,
          children: []
        },
        {
          start: "2023-04-03",
          end: "2023-04-06",
          name: "测试任务2",
          id: "2",
          progress: 0,
          task: "50%",
          date: 3,
          children: []
          // dependencies: '1'
        },
        {
          start: "2023-04-04",
          end: "2023-04-08",
          name: "测试任务3",
          id: "3",
          progress: 0,
          task: "50%",
          date: 3,
          children: []
          // dependencies: '1'
        },
        {
          start: "2023-04-08",
          end: "2023-04-09",
          name: "测试任务4",
          id: "4",
          progress: 0,
          task: "50%",
          date: 3,
          children: []
          // dependencies: '2'
        },
        {
          start: "2023-04-08",
          end: "2023-04-10",
          name: "测试任务5",
          id: "5",
          progress: 50,
          task: "50%",
          date: 3,
          children: []
          // dependencies: '2'
        }
      ],
      gantt: null,
      ganttData: null, // 甘特图数据
    });
    let handleADD = () => {
      console.log("新增按钮点击");
      vueConfig.tasks.push({
        start: "2023-04-08",
        end: "2023-04-10",
        name: "测试任务6",
        id: "6",
        progress: 0,
        task: "50%",
        date: 3
        // dependencies: '2'
      });
      createG();
    };
    let handleEdit = item => {
      console.log("编辑按钮点击");
      vueConfig.tasks.forEach(element => {
        if (element.id === item.id) {
          element.start = "2022-04-02";
          element.end = "2022-04-07";
          element.date = 5;
          element.task = "60%";
        }
      });
      createG();
    };
    let handleAddChild = (index, item) => {
      console.log("添加子任务按钮点击");
      console.log(index, item);
      vueConfig.tasks.forEach(element => {
        if (element.id === item.id) {
          element.children.push({
            start: "2022-04-01",
            end: "2022-04-08",
            name: "测试任务子任务1",
            id: "8",
            progress: 0,
            task: "50%",
            date: 3,
            dependencies: "1"
          });
        }
      });
      createG();
    };
    let handlemove = (index, item) => {
      console.log("上移一行按钮点击");
      const tempItem = vueConfig.tasks.splice(index, 1);
      vueConfig.tasks.splice(index - 1, 0, tempItem[0]);
      createG();
    };

    let formatGantt = () => {
      console.log("执行formatGantt");
      let result = [];
      let obj = {
        start: "",
        end: "",
        name: "",
        id: "",
        progress: 0,
        task: "",
        date: 0,
        children: []
      };
      vueConfig.tasks.forEach(element => {
        if (element.children.length === 0) {
          console.log(element);
          result.push(element);
        } else {
          obj.start = element.start;
          obj.end = element.end;
          obj.name = element.name;
          obj.id = element.id;
          obj.progress = element.progress;
          obj.task = element.task;
          obj.date = element.date;
          result.push(obj);
          result = result.concat(element.children);
        }
      });
      vueConfig.ganttData = result;
    };
    let createG = () => {
      formatGantt();
      const gantt = new Gantt(".gantt-target", vueConfig.ganttData, {
        on_click: function(task) {
          console.log("双击操作", task);
        },
        on_date_change: function(task, start, end) {
          vueConfig.tasks.forEach(element => {
            if (element.id === task.id) {
              element.start = start;
              element.end = end;
              element.data = end - start;
            }
          });
        },
        on_progress_change: function(task, progress) {
          console.log(task, progress);
        },
        on_view_change: function(mode) {
          console.log(mode);
        },
        // view_mode: 'Day',
        language: "zh",
        header_height: 70,
        column_width: 90,
        step: 24,
        view_modes: ["Quarter Day", "Half Day", "Day", "Week", "Month"],
        bar_height: 62,
        bar_corner_radius: 5, // bar 的圆角度
        arrow_curve: 20, //连接子任务的线条曲线度
        padding: 18,
        view_mode: "Day", // header的日期类型
        date_format: "YYYY-MM-DD", // 日期格式
        custom_popup_html: function(task) {
          return `
          <div class="details-container">
            <h5>${task.name}</h5>
            <p>Expected to finish by ${task.end}</p>
            <p>${task.progress}% completed!</p>
          </div>
          `;
        }
      });
    };
    onMounted(() => {
      createG();
    });
    return {
      ...toRefs(vueConfig),
      handleADD,
      createG,
      handleEdit,
      handleAddChild,
      handlemove,
      formatGantt,
    };
  }
};
</script>

<style lang="scss" scoped>
@import "../assets/dist/frappe-gantt.css";

.gantt-container {
  background-color: transparent;
  width: 100%;
  overflow: hidden;
  margin-left: -1px;
}


::v-deep .el-table .el-table__cell {
  height: 80px;
}

::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: rgb(245, 245, 245);
}

::v-deep .el-table--enable-row-hover .el-table__body tr:hover > td {
  background: rgb(245, 245, 245);
}

.gantt .bar {
  background-color: #007bff;
  height: 20px;
}


.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295

这样就可以实现一个简单的功能了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/499574
推荐阅读
相关标签
  

闽ICP备14008679号