Programmatically creating tasks and sub tasks

Hi,
Loving Gantt V1.3 nice job guys.
I was wondering if there was an easy way to programmatically create tasks and sub tasks and sub sub tasks etc?
I am trying to load my tasks out of a database so I am storing a parent Id of any given task. I am however running into problems. It seems like you have to create the tasks in order and the assign the tasks parent in reverse order. For example :

var project1 = new GanttProjectInfo(1, "Applet redesign", new Date(2010, 5, 11)); var parentTask2 = new GanttTaskInfo(3, "Hosted Control", new Date(2010, 6, 7), 190, 80, "1"); var parentTask5 = new GanttTaskInfo(5, "J# interfaces", new Date(2010, 6, 14), 60, 70, "6"); var parentTask123 = new GanttTaskInfo(123, "use GUIDs", new Date(2010, 6, 14), 60, 70, ""); parentTask5.addChildTask(parentTask123); parentTask2.addChildTask(parentTask5); project1.addTask(parentTask2);
so this code (from one of the examples) is:
create project
create task2
create task5
create task123
then in reverse :
Assign task 123 to task 5
Assign task 5 to task 2
Assign task 2 to project
Obviously when reading a lot of tasks out of a database in a for loop you only have the data once and looping backwards through them to assign them has to be done in another for loop.

You can assign and create data at the same time using :

parentTask2.addChildTask(new GanttTaskInfo(6, "Task D", new Date(2010, 6, 10), 30, 80, "14"));

create and assign task 6 to task 2
However this only works for sub tasks and not sub sub tasks or sub sub sub tasks and so on.

Am I missing an easy way to create and assign tasks without using 2 loops?
Hope this made sense

Cheers
Nic

Hi,
In your case, if you are not using XML or hierarchy, the best way would be like this:

  1. create all tasks:
var task01 = new GanttTaskInfo("01",...);
var task02 = new GanttTaskInfo("02",...);
...
  1. then build the hierarchy:
task02.addChildTask(task07);
...

You can create both those code parts in one loop, simply use two string variables for each of those parts. Then print them to the output.

Hi Fedor,
Thank you, in the end I used 2 for loops instead of 2 strings.
One for loop to create all the tasks after that one was finished looping another for loop to assign all of the tasks.
Cheers

Nic

@nicleek where do you get GanttTaskInfo class?

Hello Anton,
It is an old topic and related to the 1.3 version. Gantt API changed since that time.
To get information about a task, you need to use the gantt.getTask(id) method:
https://docs.dhtmlx.com/gantt/api__gantt_gettask.html
To add a task, you can use the gantt.addTask and gantt.createTask() methods:
https://docs.dhtmlx.com/gantt/api__gantt_addtask.html
https://docs.dhtmlx.com/gantt/api__gantt_createtask.html

If you want to clone tasks, you need to implement a custom solution, and here is an example that can help you to do that:
http://snippet.dhtmlx.com/5/269997138

If you need something different, please clarify.