getTask

returns the task object

Task getTask(string | number id);
idstring | numberthe task id
Taskthe task object

Example

gantt.addTask({
    id:7,
    text:"Task #5",
    start_date:"02-09-2013",
    duration:28
}, "pr_2");
 
gantt.getTask(7);
//->{id:7, text:"Task #5", start_date:"02-09-2013", duration:28, 
//   parent:"pr_2", $source:[1,5], $target:[8,13], ...}

Details

The task's object return by the getTask() method contains 2 important properties you may use to get links related to the task:

  • $source - links that comes out from the task.
  • $target - links that comes into task.

The properties are autogenerated and store ids of the coming-in and coming-out links.

const taskObj = gantt.getTask("t1");
 
const sourceLinks = taskObj.$source;        //-> ["l1","l4"] - ids of coming-out links  
const targetLinks = taskObj.$target;       //-> ["l5","l8"] - ids of coming-into links

Error

The getTask method expects a task with a required "id" is loaded into Gantt. Therefore, if no task with this "id" is found, the method will produce an error message: "Task not found id = ID".

const task = gantt.getTask("fake-id");
...

We advise you to fix causes of this error before trying to get the task object. To do that, you need to check whether the task exists via the isTaskExists method:

if(gantt.isTaskExists("fake-id")){
   const task = gantt.getTask("fake-id");
   ...
}

But you can also disable these messages before shipping your application to end users via the show_errors config:

gantt.config.show_errors = false;
See also
Back to top