vue前端实现 dhtmlxgantt 甘特图(个人需求,不完全)

打印 上一主题 下一主题

主题 1737|帖子 1737|积分 5211

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
官网地点: dhtmlx-gantt - npm   chinese-days - npm
安装:npm i dhtmlx-gantt    npm i chinese-days
开发过程中需要判断节假日并置灰显示,利用了插件chinese-days(用于查询中国节假日、调休日、工作日、24节气、以及夏历阳历互转)实现
  1. this.task = { data:[],type: "tasks"}
  2. //获取甘特图数据   
  3. public getData(data){
  4.     if(data.length){
  5.         data.forEach((val:any,index:any)=>{
  6.           this.tasks.data.push({
  7.             id: val.OBJ_ID,
  8.             name: val.TDSB,
  9.             start_date: new Date(this.timestampToTime(val.STARTTIME)),
  10.             end_date: new Date(this.timestampToTime(val.ENDTIME)),
  11.             duration: 20,
  12.             open: true, //默认打开,
  13.             toolTipsTxt:  val.TDSB,
  14.             fxdj: val.FXDJ,
  15.             index:index,
  16.             editable:true,
  17.             // progress: 0.6,
  18.             //status: "parent",
  19.           })
  20.         })
  21.       }
  22.     }   
  23. // 判断节假日并设置特殊样式,这里是给节假日设置了特殊的css类名,具体样式具需求自行添加  
  24. //  chineseDays.isHoliday -- 使用插件 判断这一日期是否为周末节假日
  25.    
  26.     public daysStyle(date: any){
  27.         if (chineseDays.isHoliday(date)) return "weekend";
  28.       };
  29.     // 根据传入数据判断甘特图渲染的颜色
  30.     public setDataColor = () => {
  31.         this.tasks.data.forEach((item: any) => {
  32.           // if (item.fxdj) {
  33.             //存在type字段 说明非一级菜单,判断阶段的具体类型 设置不同颜色
  34.             if (item.fxdj == 1) {
  35.               item.color = "rgba(255, 0, 0,.75)";
  36.               item.fxdjmc = "一级(+)";
  37.             } else if (item.fxdj == 2) {
  38.               item.fxdjmc = "一级";
  39.               item.color = "rgb(255, 136, 20,.75)";
  40.             } else if (item.fxdj == 3) {
  41.               item.fxdjmc = "二级";
  42.               item.color = "rgba(86, 146, 240,.75)";
  43.             } else if (item.fxdj == 0) {
  44.               item.fxdjmc = "无";
  45.               item.color = "rgba(0, 176, 80,.75)";
  46.             }
  47.           // }
  48.         });
  49.       };
  50.     // 初始化渲染甘特图
  51.     public initData(){
  52.         //自适应甘特图的尺寸大小, 使得在不出现滚动条的情况下, 显示全部任务
  53.         // gantt.config.autosize = "xy";
  54.         // gantt.config.open_tree_initially = true;
  55.         //   gantt.config.scale_unit = "day";
  56.         gantt.config.min_column_width = 60;
  57.         gantt.config.scale_height = 30 * 3;
  58.         gantt.config.row_height = 30;
  59.         gantt.config.drag_links = false;
  60.         gantt.config.drag_move = false;
  61.         gantt.config.drag_resize = false;
  62.         gantt.config.drag_progress = false;
  63.         gantt.config.correct_work_time = false;
  64.         gantt.config.editable = true; // 允许编辑(内联编辑)
  65.         //   是否有新增弹窗
  66.         gantt.config.details_on_create = false;
  67.         //   点击进度
  68.         gantt.config.details_on_dblclick = false;
  69.         gantt.config.order_branch = false;
  70.         gantt.config.scroll_size = 15;
  71.         // 不同字段对应的编辑配置
  72.         var textEditor = {type: "text", map_to: "name"};
  73.         var dateEditorS = {type: "date", map_to: "start_date"};
  74.         var dateEditorE = {type: "date", map_to: "start_date"};
  75.         var durationEditor = {type: "select", map_to: "fxdjmc",options: [
  76.           {label:"无",value:'0'},
  77.           {label:"一级风险(+)",value:'1'},
  78.           {label:"一级风险",value:'2'},
  79.           {label:"二级风险",value:'3'},
  80.       ]};
  81.         //   左侧目录菜单展示内容
  82.         gantt.config.columns = [
  83.           { name: "check", label: "", width: 40,
  84.             template: function(task) {
  85.                 // 创建一个复选框,并为其添加一个唯一的ID,以便后续操作
  86.                 var checkboxId = "checkbox_" + task.id;
  87.                 return "<input type='checkbox' id='" + checkboxId + "' class='gantt_checkbox' />";
  88.             },
  89.             align: "center" },
  90.           { name: "index", label: "序号", align: "center", width: 40 },
  91.           { name: "name", label: "停电设备", align: "left", width: 230, },
  92.           { name: "start_date", label: "开始时间", align: "center", width: 180 },
  93.           { name: "end_date", label: "结束时间", align: "center", width: 180  },
  94.           { name: "fxdjmc", label: "风险等级", align: "center"  },
  95.           { name: "add",label: "", onrender: (item: any) => {return " ";},},
  96.         ];
  97.         // 指向展示详情 -- 鼠标悬浮是否显示
  98.         gantt.plugins({
  99.           tooltip: true,
  100.         });
  101.         // 鼠标悬浮内容
  102.         gantt.templates.tooltip_text = function (start, end, task) {
  103.           return (
  104.             "<b>设备名称:</b> " +
  105.             task.name +
  106.             "<br/><b>开始时间:</b> " +
  107.             gantt.templates.tooltip_date_format(start) +
  108.             "<br/><b>结束时间:</b> " +
  109.             gantt.templates.tooltip_date_format(end)
  110.           );
  111.         };
  112.         gantt.config.auto_types = true;
  113.         gantt.config.xml_date = "%Y-%m-%d";
  114.         gantt.config.step = 1;
  115.         gantt.config.start_on_monday = true;
  116.         gantt.config.autoscroll = false;
  117.         gantt.config.scroll_on_click = true;
  118.         gantt.config.calendar_property = "start_date";
  119.         gantt.config.calendar_property = "end_date";
  120.         // 左侧表格宽度
  121.         gantt.config.autofit = true;
  122.         gantt.config.grid_width = 500;
  123.         //   右侧表头
  124.         gantt.config.scales = [
  125.           { unit: "month", step: 1, format: "%F, %Y" },
  126.           { unit: "week", step: 1, template: this.weekScaleTemplate },
  127.           { unit: "day", step: 1, format: "%d", css: this.daysStyle },
  128.         ];
  129.   
  130.         //   表单判断假期
  131.         gantt.templates.timeline_cell_class = (task, date): any => {
  132.           if (chineseDays.isHoliday(date)) {
  133.             return "holiday";
  134.           }
  135.         };
  136.         //   设置任务条进度内容 -- 功能不需要
  137.         // gantt.templates.progress_text = (start, end, task: any) => {
  138.         //   return (
  139.         //     "<div style='text-align:left;color:#fff;padding-left:20px'>" +
  140.         //     Math.round(task.progress * 100) +
  141.         //     "% </div>"
  142.         //   );
  143.         // };
  144.   
  145.         //任务条显示内容
  146.         gantt.templates.task_text = function (start, end, task) {
  147.           // return task.name + '(' + task.duration + '天)';
  148.           return "";
  149.         };
  150.         gantt.i18n.setLocale("cn");
  151.         //   设置进度颜色
  152.         this.setDataColor();
  153.          // 清除缓存
  154.          gantt.clearAll();
  155.         //   初始化  gantt_here 为dom的Id
  156.         gantt.init("gantt_here");
  157.         //   数据解析
  158.         gantt.parse(this.tasks);
  159.         // 监听新增事件
  160.         let _this = this;
  161.         gantt.attachEvent("onTaskCreated", function (task) {
  162.           //console.log(task, "task");
  163.           _this.flag = true;
  164.           return false;
  165.         });
  166.         // 为每个复选框添加事件监听器
  167.         gantt.eachTask(function(task:any) {
  168.           var checkboxId = "checkbox_" + task.id;
  169.           var checkbox = document.getElementById(checkboxId);
  170.           if (checkbox) {
  171.               // 添加change事件监听器
  172.               checkbox.addEventListener('change', function() {
  173.                   // 这里可以执行一些操作,比如更新任务状态或发送Ajax请求
  174.                   console.log('Task ' + task.id + ' is ' + (_this.checked ? 'checked' : 'unchecked'));
  175.               });
  176.           }
  177.         });
  178.     }
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表