Update links by sort order

Hello -

I have a group of tasks that are linked by ID from finish to start when the gantt is loaded. I have a custom column called “sort” where a user can rearrange the sort order. The sorting work, but I would like to know if there is a way to update the links between the task to match the sort order.

Thank you.

Hello,
Unfortunately, it is not completely clear for me how it works in your project.
Could you reproduce it in the following snippet?
https://snippet.dhtmlx.com/672ff07a7
After reproducing how it works, click on the “Share” button and send me the link.
Also, please tell me how do you imagine it should work.

Ok. I will phase it a different way. Let’s say I have 3 tasks that are not currently linked (see snippet: http://snippet.dhtmlx.com/80e998150). It is possible to loop through the Project subtask in their current order and link them start to finish. MS project has a function link this where you select a number of tasks and click the icon that looks like a chain and it will link the selected task start to finish.

Hello,
Thank you for the clarification.
There is no built-in feature to do that. But it is possible to iterate over all children of the selected task and create the link between them.
To do that, the first step is to get the selected task ID, then you use gantt.eachTask({code},selected_id) function. Inside that function, you check if a task has the next sibling task and link them.
Usually, when you create a link, it is enough to use +new Date as the link ID. When you create several links with a function, they might get the same IDs, because they were created at the same millisecond. Because of that, you won’t see a link, although it was added. That’s why it is necessary to add a random number as a string to avoid ID collisions.

Here is an example of how it might be implemented:
http://snippet.dhtmlx.com/4ae55a3b2
You can find more information about the functions and methods in the following articles:
https://docs.dhtmlx.com/gantt/api__gantt_getselectedid.html
https://docs.dhtmlx.com/gantt/api__gantt_addlink.html
https://docs.dhtmlx.com/gantt/api__gantt_eachtask.html
https://docs.dhtmlx.com/gantt/api__gantt_getnextsibling.html

Ramil -

Extremely helpful and I was close to finding that solution myself, but your method was a bit cleaner. I am using this code to “recreate” links under a parent based on a custom sort order. So, I have a column called “sort order”, user fills in numbers, pushes button, it sorts the children tasks under the parent, then creates the links then uses autoscheduler to move the linked tasks to the proper day based on the links. All this works great.

But if the user decide to change the sort order a second time, I need to delete the links first. I have the following code to delete ALL the links:

                   var links = gantt.serialize().links; 					
		for(var i=0;i<links.length; i++){                       
			var linkId = links[i].id;
			gantt.deleteLink(linkId);
		}

But is there a way to only delete the links for the child tasks under the selected parent?

Thank you.

Hello,
Yes, it is possible to do that.
You just need to get all links of each subtask and delete them. All links, that originate from a task, are saved in the $source property.
Here is the updated snippet:
http://snippet.dhtmlx.com/9e6a2974f