使用 ReactGantt 中的 DHTMLX Gantt 属性
本页概述了 React Gantt 支持的 props,并解释了它们与 DHTMLX Gantt 功能的对应关系。
可用 Props
| Prop | 类型 | 说明 |
|---|---|---|
| tasks | Task[] | 一个 任务对象数组。 |
| links | Link[] | 一个 链接对象数组。 |
| templates | GanttTemplates | 重写 gantt.templates,如 task_text、task_class、scale_cell_class。 |
| config | GanttConfig | 合并到 gantt.config,包含如 scales、columns、autosize 等选项。 |
| resources | Resource[] | 一个 资源对象数组。 |
| baselines | Baseline[] | 一个 基线对象数组。 |
| markers | Marker[] | 用于 时间线标记的标记对象数组。 |
| plugins | GanttPlugins | 需启用的 Gantt 扩展(如 critical_path、auto_scheduling 等)。 |
| data | ( load?: string, save?: string|RouterFunction, batchSave?: BatchChanges) | 支持通过内置 Gantt 传输加载数据,并提供回调以处理 Gantt 数据的更改。 |
| locale | string | 设置 gantt.i18n.setLocale(locale)。默认值为 "en"。 |
| theme | string | 应用 gantt.setSkin(theme)。默认值为 "terrace"。 |
| customLightbox | ReactElement | null | 用于替换默认 Lightbox 的 React 组件(参见 自定义 Lightbox)。 |
| inlineEditors | ( [editorType: string]: React.ComponentType ) | 允许将基于 React 的内联编辑器映射到 DHTMLX 的内联编辑器接口。 |
| groupTasks | GroupConfig | boolean | null | 指定分组配置,或设置为 false 或 null 以禁用分组(参见 任务分组)。 |
| filter | ((task: Task) => boolean) | null | 用于过滤显示的 Gantt 任务的函数。 |
| resourceFilter | ((resource: Resource) => boolean) | null | 过滤 资源面板中显示的资源。 |
| modals | GanttModals | 允许自定义组件替换 onBeforeTaskDelete 和 onBeforeLinkDelete 弹窗。 |
| (Event Props) | Function | 支持与 DHTMLX Gantt 事件同名的事件处理 props,例如 onTaskClick、onAfterTaskAdd 等。名称匹配的 props 会自动绑定。 |
使用示例
<ReactGantt
tasks="{tasks}"
links="{links}"
theme="material"
locale="en"
config="{" {
scales: [
{ unit: "year", step: 1, format: "%Y" },
{ unit: "month", step: 1, format: "%M" }
],
columns: [
{ name: "text", tree: true, width: '*' },
{ name: "start_date", align: "center" },
{ name: "duration", align: "center" },
{ name: "add" }
],
// 你想要的其他 gantt.config
} }
onTaskClick="{(id," e) => {
console.log('Task clicked:', id);
return true;
}}
templates="{" {
task_text: (start, end, task) => `#${task.id}: ${task.text}`,
} }
/>
事件 Props 的使用
任何 DHTMLX Gantt 事件都可以作为 prop 传递。例如:
<ReactGantt
onTaskClick="{(id," e) => {
console.log('Task clicked:', id);
return true;
}}
/>
当你传递如 onBeforeTaskAdd 这样的 prop 时,封装组件会在内部调用 gantt.attachEvent("onBeforeTaskAdd", handler)。完整事件列表请参考 DHTMLX Gantt API。
组合 Props 与 DHTMLX API
@dhx/react-gantt 库旨在通过声明式 props(如 tasks、links、resources、templates 等)覆盖大多数日常需求。但在某些情况下,您可能需要更深入地访问 Gantt 引擎,例如:
针对这些场景,有两种方式可以直接与底层 DHTMLX Gantt 功能交互:
-
React hooks:由封装库提供,连接到 Gantt 的数据存储和调度逻辑
-
通过 ref 直接访问 Gantt 实例(如果 hooks 无法满足所有需求)