使用甘特图样式
dhtmlxGantt 提供了一大组选项来修改其外观。您既可以通过使用预定义皮肤来 改变甘特图的总体外观,也可以调整组件各个元素(任务、链接、刻度、网格等)的样式。
在本指南中,我们将关于甘特图各部分样式的通用说明汇总在一起,便于您在文档中浏览。每个具体元素的详细信息在相关的文章中提供。
网格样式
您可以通过相关的 网格模板 更改网格区域的样式。
网格列的表头
有一个 grid_header_class 模板,允许您为网格列的表头应用自定 义样式。例如,您可以通过以下方式更改某些网格列表头的背景色:
<style>
.gantt-grid__header--highlighted {
background-color: #ffeb8a !important;
}
</style>
gantt.templates.grid_header_class = (columnName) =>
(columnName === 'duration' || columnName === 'text')
? 'gantt-grid__header--highlighted'
: '';

相关示例: Styling Headers of Grid Columns
自定义网格头部元素
可以在网格头部添加自定义元素(如按钮、图标、输入框等)。要添加元素,需要将其 HTML 设置为在 gantt.config.columns 配置选项中的 label 属性的值:
gantt.config.columns = [
{
name: "text",
label: `<div className="gantt-grid__header-search-wrapper">Task name
<input id="task-search" type="text" placeholder="Search tasks..."/>
</div>`,
width: 250, tree: true
},
// other columns
];
搜索功能的实现如下所示:
const taskSearchInput = document.getElementById('task-search');
taskSearchInput.addEventListener('input', () => {
gantt.refreshData();
});
function hasSubstring(parentId, searchValue) {
const task = gantt.getTask(parentId);
if (!task) return false;
if (task.text.toLowerCase().includes(searchValue)) {
return true;
}
const children = gantt.getChildren(parentId);
for (let i = 0; i < children.length; i++) {
if (hasSubstring(children[i], searchValue)) {
return true;
}
}
return false;
}
gantt.attachEvent('onBeforeTaskDisplay', (id) => {
const searchValue = taskSearchInput.value.toLowerCase().trim();
if (!searchValue) return true;
return hasSubstring(id, searchValue);
});

相关示例: Custom Elements in Grid Header