<template>
<div>
<div ref='ganttRef'></div>
<section style='display: flex;justify-content: start;'>
<pre>
{{gantttt.data}}
</pre>
<pre>
{{gantttt.links}}
</pre>
</section>
</div>
</template>
<script>
import { gantt } from 'dhtmlx-gantt'
import 'dhtmlx-gantt/codebase/dhtmlxgantt.css'
export default {
data() {
return {
gantttt: {
data: [
{id:1,text:'xx管理系统开发',person:'张三',progress:0.2,type:'task',start_date:new Date('2023-10-02'),end_date:new Date('2023-10-12'),open: true},
{id:11,parent:1,text:'初始化项目',person:'李四',type:'task',progress:0.9,color:'#ff0000',start_date:new Date('2023-10-02'),end_date:new Date('2023-10-05'),open: true},
{id:12,parent:1,text:'前后端开发',type:'task',progress:0,color:'#00ff00',start_date:new Date('2023-10-05'),end_date:new Date('2023-10-08')},
{id:13,parent:1,text:'测试',type:'task',progress:0,color:'#0000ff',start_date:new Date('2023-10-08'),end_date:new Date('2023-10-10')},
{id:14,parent:1,text:'上线',type:'task',progress:0,color:'#00ffff',start_date:new Date('2023-10-10'),end_date:new Date('2023-10-12')},
{id:111,parent:11,text:'创建git仓库',type:'task',progress:1,color:'#880000',start_date:new Date('2023-10-02'),end_date:new Date('2023-10-03')},
{id:112,parent:11,text:'搭建脚手架',type:'task',progress:0.5,color:'#550000',start_date:new Date('2023-10-03'),end_date:new Date('2023-10-04')},
{id:113,parent:11,text:'完成初始化',type:'task',progress:0.1,color:'#330000',start_date:new Date('2023-10-04'),end_date:new Date('2023-10-05')},
{id:121,parent:12,text:'前端开发',person:'甲',type:'task',progress:0,color:'#00aa00',start_date:new Date('2023-10-05'),end_date:new Date('2023-10-07')},
{id:122,parent:12,text:'后端开发',person:'已',type:'task',progress:0,color:'#007700',start_date:new Date('2023-10-05'),end_date:new Date('2023-10-07')},
{id:123,parent:12,text:'前后端对接',person:'甲、已',type:'task',progress:0,color:'#003300',start_date:new Date('2023-10-07'),end_date:new Date('2023-10-08')},
],
links: [
{ id: 1, source: 11, target: 12, type: '0' },
{ id: 2, source: 12, target: 13, type: '0' },
{ id: 3, source: 13, target: 14, type: '0' },
{ id: 4, source: 111, target: 112, type: '0' },
{ id: 5, source: 112, target: 113, type: '0' },
{ id: 6, source: 121, target: 123, type: '0' },
{ id: 7, source: 122, target: 123, type: '0' },
]
},
ganttColumns:[
{ align: 'left', name: 'text', label: '', tree: true, width:"*",min_width:120,max_width:180},
{ align: 'center', name: 'person', label: '负责人', width: '120' },
{ align: 'center', name: 'progress', label: '进度', width: '120',template:(task)=> task.progress * 100 + '%' },
]
}
},
created() {
},
mounted() {
gantt.clearAll();
gantt.plugins({
marker: true,
});
gantt.attachEvent("onTaskClick", function(id,e) {
if(e.target.className === 'gantt_task_content'){
console.log(id,e.target)
}
return true;
});
gantt.config.work_time = true;
gantt.i18n.setLocale('cn');
gantt.config.readonly = true;
gantt.config.bar_height = 32;
gantt.config.autosize = true;
gantt.config.open_split_tasks = true;
gantt.config.resize_rows = true;
gantt.config.order_branch = true;
gantt.config.order_branch_free = true;
gantt.config.scale_height = 32;
gantt.config.sort = false;
gantt.config.columns = this.ganttColumns;
gantt.config.scales = [
{ unit: 'week', step: 1, format:this.weekScaleTemplate } ,
{ unit: 'day', step: 1, format:this.formatWeekday } ,
];
gantt.init(this.$refs.ganttRef);
gantt.parse(this.gantttt)
},
methods:{
weekScaleTemplate(date){
let dateToStr = gantt.date.date_to_str("%d");
let endDate = gantt.date.add(gantt.date.add(date, 1, "week"), -1, "day");
const weekNum = gantt.date.date_to_str('%Y年%M 第%W周 ');
return weekNum(date)
},
formatWeekday(date) {
const dateToStr = gantt.date.date_to_str("%d");
const weekdays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
return dateToStr(date) + ' ' + weekdays[date.getDay()];
},
setScaleConfig(level) {
switch (level) {
case "day":
gantt.config.scales = [
{unit: "day", step: 1, format: "%d %M"}
];
gantt.config.scale_height = 27;
break;
case "week":
var weekScaleTemplate = function (date) {
var dateToStr = gantt.date.date_to_str("%d %M");
var endDate = gantt.date.add(gantt.date.add(date, 1, "week"), -1, "day");
return dateToStr(date) + " - " + dateToStr(endDate);
};
gantt.config.scales = [
{unit: "week", step: 1, format: weekScaleTemplate},
];
gantt.config.scale_height = 27;
break;
case "month":
gantt.config.scales = [
{unit: "month", step: 1, format: "%F, %Y"},
];
gantt.config.scale_height = 27;
break;
case "year":
gantt.config.scales = [
{unit: "year", step: 1, format: "%Y"},
];
gantt.config.scale_height = 27;
break;
}
}
}
}
</script>
<style>
</style>
<style lang='less' scoped>
</style>