网格中的内联编辑
dhtmlxGantt 提供两种编辑内容的选项:
- 通过使用 Lightbox 编辑表单
- 通过在 Grid 区域中使用内联编辑器
内联编辑让你直接在网格中进行任何修改:创建与更新任务,设置它们之间的连接,定义开始和结束日期,或修改持续时间——全部通过内置编辑器完成。

要启用内联编辑,你需要:
- 指定编辑器配置列表,并使用编辑器对象的 map_to 属性将所需的编辑器映射到网格列
const textEditor = { type: "text", map_to: "text" };
const dateEditor = { type: "date", map_to: "start_date", min: new Date(2025, 0, 1),
max: new Date(2026, 0, 1) };
const durationEditor = { type: "number", map_to: "duration", min: 0, max: 100 };
- 在列配置中使用 editor 属性来定义该列应使用的编辑器
gantt.config.columns = [
{ name: "text", tree: true, width: "*", editor: textEditor, resize: true },
{ name: "start_date", align: "center", editor: dateEditor, resize: true },
{ name: "duration", align: "center", editor: durationEditor },
{ name: "add", width: 44 }
];
请在 Inline Editors Extension 文章中查看 inlineEditors 对象 API 的详细信息。
你也可以查看视频指南,了解如何在网格中实现内联编辑。
编辑器的类型
内联编辑器存储在 editor_types 配置对象中。
有几种预定义的内联编辑器:
- text 编辑器 - 用于编辑文本列,例如任务名称
- number 编辑器 - 用于编辑数字列,例如任务持续时间、顺序等
- duration 编辑器 - 用于编辑持续时间列,即任务持续时间。仅当使用 map_to: "duration" 配置且编辑器类型设为 "duration" 时才起作用:
{ type: "duration", map_to: "duration", formatter: formatter }
如果你需要指定包含数字和 duration unit 的持续时间,这种内联编辑器很有用。
例如:5 days。
默认使用 Duration Formatter。
除了使用默认的持续时间格式化器,你也可以修改其配置或设置一个 自定义格式化器。
- date 编辑器 - 用于编辑日期列,例如任务的开始和结束日期
- select 编辑器 - 用于从列表中选择一个选项
- predecessor 编辑器 - 用于为当前正在编辑的任务设置前置任务连接。该编辑器会获取任务的 WBS 编码 来与前置任务建立连接。
const editors = {
text: { type: "text", map_to: "text" },
start_date: { type: "date", map_to: "start_date", min: new Date(2025, 0, 1),
max: new Date(2026, 0, 1) },
end_date: { type: "date", map_to: "end_date", min: new Date(2025, 0, 1),
max: new Date(2026, 0, 1) },
duration: { type: "number", map_to: "duration", min: 0, max: 100 },
priority: { type: "select", map_to: "priority",
options: gantt.serverList("priority") },
predecessors: { type: "predecessor", map_to: "auto" }
};
Dates limits in the Date editor
从 v6.3 开始,date 内联编辑器的最小值和最大值没有默认限制。
如果你想让在时间刻度上可见的日期限制date 内联编辑器的最小值和最大值(除非提供了自定义的 min/max 值),你可以指定动态的 min/max 值:
const dateEditor = {
type: "date",
map_to: "start_date",
min: taskId => gantt.getState().min_date,
max: taskId => gantt.getState().max_date
};
Editor for inclusive end dates
如果你使用任务的 包含结束日期的格式 并希望使其在网格中的内联编辑工作正常,你需要为编辑包含结束日期的特殊编辑器,如下所示:
// 包含 性结束日期编辑器
// 使用默认编辑器,但重写 set_value/get_value 方法
const dateEditor = gantt.config.editor_types.date;
gantt.config.editor_types.end_date = gantt.mixin({
set_value: function(value, id, column, node) {
const correctedValue = gantt.date.add(value, -1, "day");
return dateEditor.set_value.apply(this, [correctedValue, id, column, node]);
},
get_value: function(id, column, node) {
const selectedValue = dateEditor.get_value.apply(this, [id, column, node]);
return gantt.date.add(selectedValue, 1, "day");
}
}, dateEditor);
const textEditor = { type: "text", map_to: "text" };
const startDateEditor = { type: "date", map_to: "start_date" };
const endDateEditor = { type: "end_date", map_to: "end_date" };
const durationEditor = { type: "number", map_to: "duration", min: 0, max: 100 };
gantt.config.columns = [
{ name: "text", label: "Name", tree: true, width: 200, editor: textEditor,
resize: true },
{ name: "duration", label: "Duration", width: 80, align: "center",
editor: durationEditor, resize: true },
{ name: "start_date", label: "Start", width: 140, align: "center",
editor: startDateEditor, resize: true },
{ name: "end_date", label: "Finish", width: 140, align: "center",
editor: endDateEditor, resize: true }
];
// 更新 lightbox 和 grid 模板以显示包含性结束日期
gantt.templates.task_end_date = date =>
gantt.templates.task_date(new Date(date.valueOf() - 1));
const gridDateToStr = gantt.date.date_to_str("%Y-%m-%d");
gantt.templates.grid_date_format = (date, column) =>
column === "end_date"
? gridDateToStr(new Date(date.valueOf() - 1))
: gridDateToStr(date);
相关示例 Inclusive end date editor
有关结束日期格式化的更多细节,请参阅 Task end date display & Inclusive end dates 文章。
Formatting values of the Predecessor editor
此功能仅在 PRO 版中可用。
从 v6.3 开始,Gantt 允许直接在内联编辑器中指定链接类型以及延迟/提前值。
要实现这一点,你需要使用 Link Formatter 模块,并将一个 LinksFormatter 实例提供给 predecessor 编辑器:
const formatter = gantt.ext.formatters.durationFormatter({
enter: "day",
store: "day",
format: "auto"
});
const linksFormatter = gantt.ext.formatters.linkFormatter({
durationFormatter: formatter
});
const editors = {
text: { type: "text", map_to: "text" },
start_date: { type: "date", map_to: "start_date", min: new Date(2025, 0, 1),
max: new Date(2026, 0, 1) },
end_date: { type: "date", map_to: "end_date", min: new Date(2025, 0, 1),
max: new Date(2026, 0, 1) },
duration: { type: "duration", map_to: "duration", min: 0,
max: 100, formatter: formatter },
priority: { type: "select", map_to: "priority",
options: gantt.serverList("priority") },
predecessors: { type: "predecessor", map_to: "auto", formatter: linksFormatter }
};
gantt.config.columns = [
{ name: "wbs", label: "#", width: 60, align: "center", template: gantt.getWBSCode },
{ name: "text", label: "Name", tree: true, width: 200,
editor: editors.text, resize: true },
{ name: "start_date", label: "Start", width: 80, align: "center",
editor: editors.start_date, resize: true },
{ name: "predecessors", label: "Predecessors", width: 80, align: "left",
editor: editors.predecessors, resize: true, template: task => {
const links = task.$target || [];
const labels = links.map(id => {
const link = gantt.getLink(id);
return linksFormatter.format(link);
});
return labels.join(", ");
}
},
{ name: "add" }
];
Inline editing - keyboard navigation mode
下面的章节为以下自定义编辑器提供代码示例: