<template>
<div class="gunter">
<div ref="gantt" class="gunter-content" />
</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',
type: gantt.config.types.task,
progress: 1
}, {
id: 12,
text: '任务2',
type: gantt.config.types.task,
start_date: '19-04-2018',
end_date: '22-04-2018',
progress: 0.6
}, {
id: 13,
text: '任务3',
start_date: '22-04-2018',
end_date: '30-04-2018',
type: gantt.config.types.task,
progress: 0
}, {
id: 14,
text: '任务4',
type: gantt.config.types.task,
start_date: '25-04-2018',
end_date: '28-04-2018',
progress: 0.8
}, {
id: 15,
text: '任务5',
type: gantt.config.types.task,
start_date: '29-04-2018',
end_date: '02-05-2018',
progress: 0.2
}, {
id: 16,
text: '任务6',
start_date: '02-05-2018',
end_date: '03-05-2018',
type: gantt.config.types.task,
progress: 0
}, {
id: 17,
text: '任务7',
start_date: '02-05-2023',
end_date: '03-05-2023',
type: gantt.config.types.task,
progress: 0
}
]
}
}
},
mounted() {
this.init()
},
methods: {
init() {
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.config.resize_rows = false
gantt.config.grid_resize = true
gantt.config.grid_elastic_columns = true
gantt.config.show_task_cells = false
gantt.config.show_errors = false
gantt.plugins({
tooltip: true
})
gantt.templates.tooltip_text = function(start, end, task) {
return `<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.templates.task_class = function(start, end, task) {
console.log(task.type, task.colorType, task.status, task.parent)
return `gantt_${task.type}`
}
gantt.templates.date_grid = function(date, task, column) {
console.log('日期网格', date, task, column)
if (task && gantt.isUnscheduled(task) && gantt.config.show_unscheduled) {
return gantt.templates.task_unscheduled_time(task)
} else {
return gantt.templates.grid_date_format(date)
}
}
gantt.templates.timeline_cell_class = function(item, date) {
console.log(item, date)
if (date.getDay() === 0 || date.getDay() === 6) {
return 'weekend'
}
}
gantt.templates.task_text = function(start, end, task) {
return `${task.text}`
}
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.config.lightbox.sections = [
{
name: 'description',
height: 70,
map_to: 'text',
type: 'textarea',
focus: true
},
{ name: 'type', type: 'typeselect', map_to: 'type' },
{ name: 'time', type: 'duration', map_to: 'auto' }
]
gantt.init(this.$refs.gantt)
gantt.parse(this.tasks)
}
}
}
</script>
<style lang="scss">
.gantt_task {
border: none;
background: #79bbff;
}
.gantt_task_row.gantt_selected {
background: #f0f3f9;
}
.gantt_grid_data .gantt_row.gantt_selected {
background: #f0f3f9;
}
.gantt_grid_data .gantt_row.odd.gantt_selected {
background: #f0f3f9;
}
.gantt_grid_data .gantt_row.odd:hover, .gantt_grid_data .gantt_row:hover {
background: #f0f3f9;
}
.gantt_task .gantt_task_progress {
background: #53a8ff;
}
.gantt_task.gantt_selected {
background: #53a8ff;
}
</style>