Getting the Link Object/Id

To work with links in the Gantt chart, you need to know how to get the object or id of a link. First of all, most methods take the link's object(id) as a parameter. Secondly, custom scenarios for links can't be implemented without referring to the link's object(id).

Getting the link object

To get a link object, use the getLink method:

gantt.getLink("link1");                //-> {id:"link1", source:1, target:2, type:1}

Getting all links from the Gantt chart

To get all links presented in the chart, use the getLinks method as in:

var links = gantt.getLinks();

It will return an array of links' objects.

Getting the links related to a certain task

To get links related to a task, use the $source, $target properties of the task's object.
The properties are autogenerated and store ids of the related links:

  • $source - links that comes out from the task.
  • $target - links that comes into task.
var taskObj = gantt.getTask("t1");
 
var sourceLinks = taskObj.$source;        //-> ["l1","l4"] - ids of coming-out links  var targetLinks = taskObj.$target;       //-> ["l5","l8"] - ids of coming-into links

Getting the link id

Generally, you can get the id of a link from the links object of the data set.

{
    data:[...],
    links:[
        { id:1, source:1, target:2, type:"1"},                       //link's id = 1         { id:2, source:2, target:3, type:"0"},                       //link's id = 2         { id:3, source:3, target:4, type:"0"}                        //link's id = 3     ]
}


If you want to get the id of link(s) with a specific "target", "source" or "type" value, use the technique as in:

//searching for a link that goes from the task with id=1 to the task with id=2
var links = gantt.serialize().links;                             //returns all links
for(var i=0;i<links.length; i++){                              //goes over all links
   if ( (links[i].source == 1) && (links[i].target == 2) )
       var linkId = links[i].id;
};

Changing the link id

To change the current id of a link, use the changeLinkId method:

gantt.changeLinkId(1274, "link14");          //changes the link id: 1274 -> "link14"
Back to top