[Question] Build object tasks for gantt.parse

The question is pretty trivial, but my level of programming doesn’t allow me to solve it.

I’m trying to build an object, for display.
Built it first with simple push method, then I realized there is an error and something is wrong.

Step by step, first I make ajax request to api server. I get data array, then I form object during enumeration. Example:

data = [];
$.ajax(settings).done(function (response) {
  obj = response.result.tasks;
  Object(obj).forEach(function (entry, index) {
    console.log(entry);
    startDatePlan = entry.startDatePlan.substring(0, 10);
    endDatePlan = entry.endDatePlan.substring(0, 10);
data.push ({id: entry.id, text:entry.title, start_date: startDatePlan, end_date: endDatePlan})    
  })
}); 

The output I get is an array like this:

[
    {
        "id": "7852",
        "text": "D#3628 7852",
        "start_date": "2021-10-21",
        "end_date": "2021-11-05"
    },
    {
        "id": "7854",
        "text": "D#3628 7854",
        "start_date": "2021-10-06",
        "end_date": "2021-10-21"
    },
    {
        "id": "7856",
        "text": "D#3628 7856",
        "start_date": "2021-09-28",
        "end_date": "2021-10-06"
    }
]

But this array doesn’t work, having compared it to the demo version. I see that my properties are not defined as numbers and dates. But I can’t figure out how to build the array correctly.

I even tried filling the object and pushing it into the array. But I faced with indices, which are not used in the demo.

gantt.parse({
    data:[
        {id:1, text:"Project #2", start_date:"01-04-2013", duration:18},
        {id:2, text:"Task #1",    start_date:"02-04-2013", duration:8,
            progress:0.6, parent:1},
        {id:3, text:"Task #2",    start_date:"11-04-2013", duration:8,
            progress:0.6, parent:1}
    ],
    links:[
        { id:1, source:1, target:2, type:1},
        { id:2, source:2, target:3, type:0}
  ]
});

Hello,
Gantt expects the following object with tasks and links:

{
  data:[
    {},
    {},
    {},
  ],
  links:[
    {},
    {},
    {},
  ]
}

Instead of the data property, you can also use the tasks property:
https://docs.dhtmlx.com/gantt/desktop__loading.html
So, you need to create an object with that structure.
Here is an example of how it can be done:
http://snippet.dhtmlx.com/5/d2ad4ed45


I even tried filling the object and pushing it into the array. But I faced with indices, which are not used in the demo.

Please, clarify which indices you mean. To make it more clear, you can send a screenshot.

1 Like

I try build

o = {tasks: obj_data}

Final object:
{
“tasks”: [
{
“id”: 7898,
“text”: “Task WH”,
“start_date”: “2021-09-29”,
“end_date”: “2021-09-30”
}
]
}
and nothing, table empty, without error

Please, clarify which indices you mean. To make it more clear, you can send a screenshot.

That’s what I meant.
The first array is demo data, essentially manually filled in.
The second array is the one I collected by code, as you can see in my case it doesn’t specify the number of elements. From this I deduced that there is some problem with the validity of the array.

Because if I copy the object and paste what you showed in the demo link then everything is ok. But if the array is built programmatically, it doesn’t work.

Perhaps I was tired or very clogged up the code.
Now, rewrote all the calls from scratch and in the output yes, everything worked. The array, which I programmatically assembled from the query eventually drew a diagram.

Question closed

1 Like