数据模型
Gantt 使用两种主要表示方式来处理任务和连线数据:
- Serialized: JSON 兼容的结构,日期为字符串,用于服务器响应、持久化 JSON,以及 DataProcessor 交换
- Runtime: 客户端对象,包含
Date字段和以$前缀的计算属性,由诸如 gantt.getTask() 与 gantt.getLink() 这样的方法返回
当你向 Gantt 提供数据(而不是读取回数据)时,日期字段可以是 Date 或 string。 TaskInput 类型捕捉了这种宽松的输入形状,因此在你生成或在应用状态中保存的数据不必固定为 Task 或 SerializedTask。
传递给 gantt.parse() 的规范顶层载荷是 GanttData。
核心运行时类型和序列化类型都从 @dhx/gantt 导出。包装包会在其公共 API 中重新导出并使用这些类型,但具体的属性表面在不同包装之间略有差异。
数据生命周期
数据通过两次变换流动:
- 加载(Loading):序列化的任务和连线数据传给
gantt.parse()或gantt.load()。Gantt 会将日期字符串解析为Date对象,并添加以$开头的计算字段,生成运行时的Task和Link对象。 - 保存(Saving):当更改通过 DataProcessor 发送到服务器时,日期会被重新序列化为字符串,临时的
$-前缀字段会被剥离。
有关行为细节,请参阅 Data Loading 和 Server-Side Integration。
SerializedTask
JSON 兼容的任务形状。日期字段为字符串,因此此对象可以安全地经过 JSON.stringify() / JSON.parse()。
interface SerializedTask {
id?: string | number;
start_date?: string;
end_date?: string;
duration?: number;
text?: any;
type?: string;
parent?: string | number;
progress?: number;
open?: boolean;
auto_scheduling?: boolean;
unscheduled?: boolean;
constraint_date?: string;
constraint_type?: string;
deadline?: string;
color?: string;
textColor?: string;
progressColor?: string;
bar_height?: number;
row_height?: number;
hide_bar?: boolean;
baselines?: SerializedBaseline[];
calendar_id?: string | number;
editable?: boolean;
readonly?: boolean;
render?: string;
resource?: string[];
rollup?: boolean;
target?: string;
[customProperty: string]: any;
}
要获得一个在序列化 JSON 中有意义的计划任务,请提供以下其中一个有效的排程组合:
start_date+durationstart_date+end_dateduration+end_date
如果 unscheduled: true,日期可以省略。
有关更详细的属性描述,请参阅 Task Properties。
SerializedLink
interface SerializedLink {
id: string | number;
source: string | number;
target: string | number;
type: string;
lag?: number;
readonly?: boolean;
editable?: boolean;
[customProperty: string]: any;
}
有关详细的属性描述,请参阅 Link Properties。
Runtime Task 与 Link
加载完成后,Gantt 将任务以运行时的 Task 对象存储。
与 SerializedTask 的主要区别: