任务对象/Id
在甘特图中处理数据时,了解如何访问数据项的对象或id非常重要。大多数方法都需要数据对象或id作为参数。此外,任何与数据相关的操作都依赖于引用数据对象或id。
有关任务的树结构相关方法,请参阅 Task Parent/Child 文章。
任务对象
要获取任务对象,请使用 getTask 方法:
gantt.getTask("t1");
//->{id:"t1", text:"Task #5", start_date:"02-09-2020", duration:28,
// progress:0.6, parent:"pr_2", $source:[3,5], $target:[2,1], ...}
任务的父任务
要查找任务的父任务,可以使用 getParent 方法,或访问任务对象的 parent 属性:
gantt.getParent("t1"); //->"pr_2"。如果没有父任务,该方法返回根id
//或
var taskObj = gantt.getTask("t1"); //-> {id:"t1", text:"Task #5", parent:"pr_2", ...}
var taskParent = taskObj.parent; //-> "pr_2"
有关甘特图树结构的所有相关方法,请参阅 Task Parent/Child 文章。
与任务相关的链接
要了解如何获取与特定任务相关的所有链接,请查阅 링크 객체/ID 가져오기 文章。
任务时长
要确定任务的持续时长,请使用 calculateDuration 方法:
gantt.calculateDuration(new Date(2020,03,30),new Date (2020,04,02)); // ->16
如果只更改了 duration 参数并更新了任务对象,该方法将无法正确工作。为确保其正常工作,必须同时使用 calculateEndDate 方法更新 end_date 参数。参见示例。
注意,如果启用了 work_time 选项,calculateDuration 方法会根据工作时间计算任务时长。
任务高度
要获取任务DOM元素的高度,请使用 getTaskBarHeight 方法:
gantt.config.bar_height = 45;
gantt.render();
gantt.getTaskBarHeight(1); // -> 45
返回值也可以对应于任务对象上设置的 bar_height 属性:
var tasks = {
data:[
{ id: 1, text: "Project #2", start_date: "01-04-2018", duration: 18,
progress: 0.4, open: true, bar_height: "full", row:height: 50 },
{ id: 2, text: "Task #1", start_date: "02-04-2018", duration: 8,
progress: 0.6, parent: 1, bar_height: 25, row:height: 50 },
]
};
gantt.init("gantt_here");
gantt.parse(tasks);
gantt.getTaskBarHeight(1); // -> 45
gantt.getTaskBarHeight(2); // -> 25
注意,当 bar_height 属性设置为 "full" 时,该方法会以像素为单位计算任务条高度。