Task Parent/Child

In this article you will find methods related to the tree nature of the Gantt chart tasks.

To learn about basic operations of getting the task object/id, refer to the Task Object/Id article.

Parent of a task

To get the parent of a task, use the getParent method or the "parent" property of the task's object:

gantt.getParent("t1");//->"pr_2"
//or
var taskObj = gantt.getTask("t1");//-> {id:"t1", text:"Task #5", parent:"pr_2", ...}
var taskParent = taskObj.parent;  //-> "pr_2"

Note, if there is no parent for the specified task, the method returns the root id.

Children of a task

To get children of a branch task, use the getChildren method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18},
     {id:"t_1", text:"Task #1",    start_date:"02-04-2020", duration:8,
     parent:"p_1"}
]};
gantt.getChildren("p_1");//->["t_1"]

To get all children of a task (not only the 1 st-level child tasks), apply the eachTask() method and pass the ID of the parent task as a second parameter:

const children = [];
// iterate through all children of a task
gantt.eachTask(function(child){
    children.push(child)
}, 11);

Checking if a task has a child

To check if some task has a child task, use the hasChild method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18, 
     open:true},
     {id:"t_1", text:"Task #1", start_date:"02-04-2020", duration:8,
     parent:"p_1"},
     {id:"t_2", text:"Task #2", start_date:"11-04-2020", duration:8,
     parent:"p_1"}
   ]
};
gantt.init("gantt_here");
gantt.parse(data);
 
gantt.hasChild("p_1"); //-> true  gantt.hasChild("t_1"); //-> false

Next task in a tree

To get the object of a task next to the specified one, use the getNext method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18, 
     open:true},
     {id:"t_1", text:"Task #1", start_date:"02-04-2020", duration:8,
     parent:"p_1"},
     {id:"t_2", text:"Task #2", start_date:"11-04-2020", duration:8,
     parent:"p_1"}
   ]
};
gantt.init("gantt_here");
gantt.parse(data);
 
gantt.getNext("p_1"); -> "t_1"   gantt.getNext("t_1"); -> "t_2"   gantt.getNext("t_2"); -> null

Note, the gantt considers tasks regardless of the tree level

Previous task in a tree

To get the object of a task next to the specified one, use the getPrev method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18, 
     open:true},
     {id:"t_1", text:"Task #1", start_date:"02-04-2020", duration:8,
     parent:"p_1"},
     {id:"t_2", text:"Task #2", start_date:"11-04-2020", duration:8,
     parent:"p_1"}
   ]
};
gantt.init("gantt_here");
gantt.parse(data);
 
gantt.getPrev("p_1"); ->  null   gantt.getPrev("t_1"); -> "p_1"   gantt.getPrev("t_2"); -> "t_1"

Note, the gantt considers tasks regardless of the tree level

Siblings of a task

To get siblings of the specified task, use the getSiblings method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18, 
     open:true},
     {id:"t_1", text:"Task #1", start_date:"02-04-2020", duration:8,
     parent:"p_1"},
     {id:"t_2", text:"Task #2", start_date:"11-04-2020", duration:8,
     parent:"p_1"}
   ]
};
gantt.init("gantt_here");
gantt.parse(data);
 
gantt.getSiblings("t_1"); ->  ["t_1", "t_2]

Next sibling of a task

To get the next sibling of the specified task, use the getNextSibling method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18, 
     open:true},
     {id:"t_1", text:"Task #1", start_date:"02-04-2020", duration:8,
     parent:"p_1"},
     {id:"t_2", text:"Task #2", start_date:"11-04-2020", duration:8,
     parent:"p_1"}
   ]
};
gantt.init("gantt_here");
gantt.parse(data);
 
gantt.getNextSibling("t_1"); ->  "t_2" gantt.getNextSibling("t_2"); ->  null (if no next sibling)

Previous sibling of a task

To get the previous sibling of the specified task, use the getPrevSibling method:

var data = {
  tasks:[
     {id:"p_1", text:"Project #1", start_date:"01-04-2020", duration:18, 
     open:true},
     {id:"t_1", text:"Task #1", start_date:"02-04-2020", duration:8,
     parent:"p_1"},
     {id:"t_2", text:"Task #2", start_date:"11-04-2020", duration:8,
     parent:"p_1"}
   ]
};
gantt.init("gantt_here");
gantt.parse(data);
 
gantt.getPrevSibling("t_2"); ->  "t_1" gantt.getPrevSibling("t_1"); ->  null (if no previous sibling)
Back to top