操作指南
如何切换网格/图表
在使用默认布局配置时,可以通过更改 show_grid 或 show_chart 参数,并调用 render() 方法来切换网格或图表的显示状态,从而更新界面。
function toggleGrid(){
gantt.config.show_grid = !gantt.config.show_grid;
gantt.render();
}
Related example: Gantt. Toggle grid (default layout)
function toggleChart(){
gantt.config.show_chart = !gantt.config.show_chart;
gantt.render();
}
Related example: Gantt. Toggle timeline (default layout)
对于自定义布局配置,需要分别创建包含或不包含网格或时间线的不同布局。切换时,需更新 gantt.config.layout 参数,并通过 init() 方法重新初始化以应用更改:
let showGrid = true;
function toggleGrid() {
showGrid = !showGrid;
if (showGrid) {
gantt.config.layout = gridAndChart; // 带有网格和时间线的布局
}
else {
gantt.config.layout = onlyChart; // 仅有时间线的布局
}
gantt.init("gantt_here");
}
Related example: Gantt. Toggle grid (custom layout)
let showChart = true;
function toggleChart() {
showChart = !showChart;
if (showChart) {
gantt.config.layout = gridAndChart; // 带有网格和时间线的布局
}
else {
gantt.config.layout = onlyGrid; // 仅有网格的布局
}
gantt.init("gantt_here");
}
Related example: Gantt. Toggle timeline (custom layout)
如何切换资源视图
与切换网格或时间线视图类似,需要准备包含或 不包含资源视图的多种布局配置。切换时需更新 gantt.config.layout 参数,并调用 init() 方法以反映更改:
let resourceChart = true;
function layoutChange() {
resourceChart = !resourceChart;
if (resourceChart) {
gantt.config.layout = resourceLayout;
}
else {
gantt.config.layout = noresourceLayout;
}
gantt.init("gantt_here");
};
Related example: Gantt. Toggle resource load diagram
let histogramView = true;
function layoutChange() {
histogramView = !histogramView;
if (histogramView) {
gantt.config.layout = histogramLayout;
}
else {
gantt.config.layout = simpleLayout;
}
gantt.init("gantt_here");
};
Related example: Gantt. Toggle resource histogram
另一种方式是使用布局视图动态生成布局,并重新初始化 Gantt 以更新显示:
Related example: Gantt. Generate layout
如何在时间线上实现无限滚动
无限滚动可以通过多种方式实现,通常涉及通过 gantt.config.start_date 和 gantt.config.end_date 参数调整显示的日期范围:
使用滚动条时
通过监听 scroll position,当用户滚动到边缘附近时,可以扩展日期范围。为避免性能问题,建议使用延时(timeout)方式重绘 Gantt 图表:
gantt.init("gantt_here");
gantt.parse(tasks);
gantt.attachEvent("onGanttScroll", function (left, top) {
const left_date = gantt.dateFromPos(left)
const right_date = gantt.dateFromPos(left + gantt.$task.offsetWidth)
gantt.config.start_date = gantt.config.start_date || gantt.getState().min_date;
gantt.config.end_date = gantt.config.end_date || gantt.getState().max_date;
const min_allowed_date = gantt.date.add(gantt.config.start_date, 1, "day");
const max_allowed_date = gantt.date.add(gantt.config.end_date, -2, "day");
let repaint = false;
if (+left_date <= +min_allowed_date) {
gantt.config.start_date = gantt.date.add(gantt.config.start_date, -2, "day");
repaint = true;
}
if (+right_date >= +max_allowed_date) {
gantt.config.end_date = gantt.date.add(gantt.config.end_date, 2, "day");
repaint = true;
}
if (repaint) {
setTimeout(function () {
gantt.render()
gantt.showDate(left_date)
}, 20)
}
});
Related example: Gantt. Infinite scroll while using scrollbar
拖动时间线时
在拖动时间线过程中检测当前滚动位置,如果接近时间线的起止端,则扩展日期范围:
gantt.attachEvent("onMouseMove", function (id, e) {
if (!gantt.getState().drag_id && e.buttons == 1) {
const left_date = gantt.dateFromPos(gantt.getScrollState().x);
const right_date = gantt.dateFromPos(
gantt.getScrollState().x + gantt.$task.offsetWidth - 1
);
if (left_date && +left_date <= +gantt.config.start_date) {
gantt.config.start_date = gantt.date.add(gantt.config.start_date, -1, 'day');
gantt.render();
}
if (right_date && +gantt.config.end_date < +gantt.date.add(right_date, 1, 'day')) {
gantt.config.end_date = gantt.date.add(gantt.config.end_date, 1, 'day');
gantt.render();
}
}
});
Related example: Gantt. Infinite scroll while dragging the timeline
拖动任务时
如果未显式设置日期范围,可在任务拖动到时间线边缘时每次调用 render() 来保持可见范围:
gantt.init("gantt_here");
gantt.parse(tasks);
gantt.attachEvent("onTaskDrag", function (id, mode, task, original) {
if (task.start_date <= gantt.getState().min_date ||
task.end_date >= gantt.getState().max_date) {
gantt.render()
}
});
Related example: Gantt. Infinite scroll while dragging a task (default range settings)
如果已显式设置 date range,则需在任务拖动到边缘时动态更新日期范围:
gantt.init("gantt_here");
gantt.parse(tasks);
gantt.config.start_date = new Date(2025, 02, 28)
gantt.config.end_date = new Date(2025, 03, 10)
gantt.render();
gantt.attachEvent("onTaskDrag", function (id, mode, task, original) {
if (+task.start_date <= +gantt.config.start_date) {
gantt.config.start_date = gantt.date.add(
gantt.config.start_date, -1, gantt.config.duration_unit
);
gantt.render()
}
if (+task.end_date >= +gantt.config.end_date) {
gantt.config.end_date = gantt.date.add(
gantt.config.end_date, 1, gantt.config.duration_unit
);
gantt.render()
}
});
Related example: Gantt. Infinite scroll while dragging a task (explicit range settings)
如何动态加载任务
通过 onGanttScroll 事件检测滚动到最后一条可见任务时,可以调用 parse() 方法动态加载更多任务:
gantt.attachEvent("onGanttScroll", function (left, top) {
const visibleTasks = gantt.getVisibleTaskCount();
const lastVisibleTask = gantt.getTaskByIndex(visibleTasks - 1)
if (gantt.getTaskRowNode(lastVisibleTask.id)) {
const tasks = load_tasks()
gantt.parse(tasks);
}
});
Related example: Gantt. Load data dynamically
如何通过按钮展开/收起所有任务
可以使用 open() 和 close() 方法分别展开或收起单个任务。若要对所有任务操作,可结合 eachTask() 方法实现。将操作包裹在 batchUpdate() 内可确保图表只重绘一次:
function collapseAll() {
gantt.batchUpdate(function () {
gantt.eachTask(function (task) {
gantt.close(task.id)
})
})
}
function expandAll() {
gantt.batchUpdate(function () {
gantt.eachTask(function (task) {
gantt.open(task.id)
})
})
}
Related example: Gantt. Add collapse/expand buttons into Gantt header
Related example: Gantt. Collapse/expand all tasks
如何在网格单元格/表头中显示多行文本
在网格表头或单元格中显示多行文本,可以通过应用特定 CSS 样式实现。
对于网格表头:
.gantt_grid_head_text{
line-height: 12px;
white-space:normal;
}
Related example: Gantt. Multiline text in the grid header
对于网格单元格:
.gantt_tree_content, .gantt_task_content{
line-height: 12px;
white-space:normal;
overflow-wrap: break-word;
}
Related example: Gantt. Multiline text in Grid cells and Timeline
Related example: Gantt. Multiline text in cells of a Grid column