I was creating a project with Gantt, and in my project I need to sync any resources in one task, but i dont know how to create the file XML for read the resource.
I saw the samples and I was follow the example but the resources is in JSON and I want to use in XML.
How can I to create a file with XML?
I am using link to, and with link I using like that
<coll_options for="links"> ... </coll_options>
But the resources I don’t know how to use
Hello,
Unfortunately, Gantt currently doesn’t have the ability to serialize resources into XML. However, you can generate the XML manually. Something like this:
function serializeDataToXML() {
let serializedXmlData = '<data>';
serializedXmlData += serializeDataEntityToXML('task');
serializedXmlData += serializeDataEntityToXML('link');
serializedXmlData += serializeDataEntityToXML('resource');
serializedXmlData += '</data>';
console.log(serializedXmlData);
}
function serializeDataEntityToXML(entityType) {
const entityStore = gantt.getDatastore(entityType);
let serializedData = '';
if (entityType !== 'task') {
serializedData += `<coll_options for='${entityType}s'>`;
}
entityStore.eachItem((item) => {
serializedData += `<${entityType === 'task' ? entityType : 'item'}`;
let taskTextValue = '';
for (const property in item) {
if (item.hasOwnProperty(property) && !property.startsWith('$')) {
let value = item[property];
if (value instanceof Date) {
const formatFunc = gantt.date.date_to_str('%d-%m-%Y %H:%i');
value = formatFunc(value);
}
if (entityType === 'task' && property === 'text') {
taskTextValue = value;
} else {
serializedData += ` ${property}='${value}'`;
}
}
}
if (entityType === 'task') {
serializedData += `><![CDATA[${taskTextValue}]]></${entityType}>`;
} else {
serializedData += ' />';
}
});
if (entityType !== 'task') {
serializedData += '</coll_options>';
}
return serializedData;
}
I’ve added button for calling the function:
<div class="gantt_control">
<input type='button' value='Serialize tasks, links and resources data' onclick='serializeDataToXML()'>
</div>
Here is an example: DHTMLX Snippet Tool
It generates the fallowing XML structure:
const xmlData = `
<data>
<task id='1' type='project' start_date='03-04-2023 00:00' duration='27' parent='0' progress='0.4' owner_id='5' end_date='30-04-2023 00:00'><![CDATA[Office itinerancy]]></task>
...
<coll_options for='links'>
...
<item id='16' source='17' target='25' type='0' />
...
</coll_options>
<coll_options for='resources'>
...
<item id='8' text='Anna' parent='2' owner_id='2' open='true' />
...
</coll_options>
</data>
`
You can upload this data to Gantt like this:
gantt.parse(xmlData, 'xml');
resourcesStore.parse(gantt.serverList('resources'))
gantt.resetLayout();
Please see an example: DHTMLX Snippet Tool