从客户端资源加载数据
data | string | DataToLoad | 表示数据的字符串或对象 |
type | string | 可选,('json', 'xml') 指定数据类型。默认值为 'json' |
gantt.parse({
data:[
{id:1, text:"Project #2", start_date:"01-04-2023", duration:18},
{id:2, text:"Task #1", start_date:"02-04-2023", duration:8,
progress:0.6, parent:1},
{id:3, text:"Task #2", start_date:"11-04-2023", duration:8,
progress:0.6, parent:1}
],
links:[
{ id:1, source:1, target:2, type:1},
{ id:2, source:2, target:3, type:0}
]
});
Gantt 期望任务数组命名为 data 或 tasks,而链接数组应命名为 links。
数据结构如下:
gantt.parse({
data: [
{ id: 1, start_date: "2025-09-23", duration: 42,
text: "House Construction" },
{ id: 2, start_date: "2025-12-02", duration: 60,
text: "House Construction" },
],
"links": [
{ id: "1", source: "1", target: "2", type: "0" },
],
"resources": [
{ id: 1, text: "Anna, Architect", unit: "hours/day",
default_value: 8, type: "work" },
],
"assignments": [
{ task_id: "1", resource_id: "1", value: "8" },
{ task_id: "2", resource_id: "1", value: "8",
mode: "fixedDates", start_date: "2025-09-23",
end_date: "2025-09-25", duration: 4, delay: 2, },
{ task_id: "2", resource_id: "1", value: "8",
start_date: new Date("2025-09-23 00:00:00"),
end_date: new Date("2025-09-26 00:00:00"), },
]
})
data 或 tasks 数组应包含 NewTask 对象,它们与 Task 对象不同。NewTask 可以是字符串或空对象。这些对象可以拥有与Task对象相同的属性,也可以添加自定义属性。与 Task 对象不同的是,以 $ 开头的属性会被忽略,日期可以是字符串。
具体说明:
这不是任务属性的完整列表,更多内容请参见此文档。
gantt.parse({
data: [
{ id: 1, start_date: "2025-09-23", duration: 42,
text: "House Construction" },
]
})
links 数组应包含 Link对象。
gantt.parse({
data: [],
links: [
{ id: "1", source: "1", target: "2", type: "0" },
]
})
resources 数组期望包含 NewResourceItem 对象,可能包括:
gantt.parse({
data: [],
resources: [
{ id: 1, text: "Anna, Architect", unit: "hours/day",
default_value: 8, type: "work" },
]
})
assignments 数组期望包含 NewAssignmentItem 对象,属性示例如下:
gantt.parse({
data: [],
assignments: [
{ task_id: "1", resource_id: "1", value: "8" },
]
})
collections 对象用于加载自定义数据。其属性名可随意,值为包含集合项的数组:
每个 СollectionItem 是一个包含任意属性的对象:
gantt.parse({
data: [
{ "id": "1", "text": "Task #1", "priority": 1,
"start_date": "02-04-2019", "duration": 1, },
{ "id": "2", "text": "Task #2", "priority": 2,
"start_date": "01-04-2019", "duration": 1, },
{ "id": "3", "text": "Task #3", "priority": 3,
"start_date": "02-04-2019", "duration": 1, },
{ "id": "4", "text": "Task #4", "priority": 1,
"start_date": "03-04-2019", "duration": 1, },
],
links: [],
collections: {
task_priority: [
{ key: 1, label: "High" },
{ key: 2, label: "Normal" },
{ key: 3, label: "Low" }
]
}
});
如果数据中不包含任务,仍需定义一个空的 tasks 数组:
gantt.parse({
tasks:[],
links:[
{ id:1, source:1, target:2, type:1},
{ id:2, source:2, target:3, type:0}
]
});
从 v8.0 版本开始,你还可以通过 parse() 方法同时加载资源和资源分配,以及任务和链接:
gantt.parse({
tasks: [
...,
{
id: 5,
text: "Interior office",
type: "task",
start_date: "03-04-2024 00:00",
duration: 7,
parent: "2",
owner: [
{
resource_id: "6",
value: 3,
start_date: "03-04-2024 00:00",
end_date: "05-04-2024 00:00",
}
]
},
...
],
links: [],
resources: [
{id: 6, text: "John", unit: "hours/day" },
{id: 7, text: "Mike", unit: "hours/day" },
{id: 8, text: "Anna", unit: "hours/day" },
{id: 9, text: "Bill", unit: "hours/day" },
{id: 10, text: "Floe", unit: "hours/day" }
]
});
更多详情请参见这里。