DHTMLX Docs & Samples Explorer

### Save and load XML data

| To use save option, you should run this sample under Apache with PHP.

Source

    <link type="text/css" rel="stylesheet" href="../../codebase/dhtmlxgantt.css">
    <script type="text/javascript" language="JavaScript" src="../../codebase/dhtmlxcommon.js"></script>
    <script type="text/javascript" language="JavaScript" src="../../codebase/dhtmlxgantt.js"></script>
    <script type="text/javascript" language="JavaScript">
    /*<![CDATA[*/
    function createChartControl(htmlDiv)
    {
        // Initialize Gantt data structures
        //project 1
        var project1 = new GanttProjectInfo(1, "Project 1 test test test test", new Date(2010, 5, 11));
        var parentTask1 = new GanttTaskInfo(1, "Project1 task 1", new Date(2010, 5, 11), 208, 50, "");
        parentTask1.addChildTask(new GanttTaskInfo(2, "Project1 task 2", new Date(2010, 5, 11), 100, 40, ""));
        parentTask1.addChildTask(new GanttTaskInfo(13, "Project1 task 13", new Date(2010, 5, 12), 80, 90, ""));
        var parentTask3 = new GanttTaskInfo(4, "Project1 task 4", new Date(2010, 5, 22), 105, 46, "13");
        parentTask3.addChildTask(new GanttTaskInfo(21, "Project1 task 21", new Date(2010, 5, 23), 70, 30, ""));
        parentTask3.addChildTask(new GanttTaskInfo(22, "Project1 task 22", new Date(2010, 5, 22), 80, 30, ""));
        parentTask3.addChildTask(new GanttTaskInfo(23, "Project1 task 23", new Date(2010, 5, 25), 70, 30, ""));
        parentTask3.addChildTask(new GanttTaskInfo(24, "Project1 task 24", new Date(2010, 5, 26), 70, 55, ""));
        parentTask1.addChildTask(parentTask3);
        var parentTask2 = new GanttTaskInfo(3, "Project1 task 3", new Date(2010, 6, 7), 190, 80, "1");
        var parentTask5 = new GanttTaskInfo(5, "Project1 task 5", new Date(2010, 6, 14), 60, 70, "6");
        var parentTask123 = new GanttTaskInfo(123, "Project1 task 12", new Date(2010, 6, 14), 60, 70, "");
        parentTask5.addChildTask(parentTask123);
        parentTask2.addChildTask(parentTask5);
        parentTask2.addChildTask(new GanttTaskInfo(6, "Project1 task 6", new Date(2010, 6, 10), 30, 80, "14"));
        var parentTask4 = new GanttTaskInfo(7, "Project1 task 7", new Date(2010, 6, 15), 118, 80, "6");
        var parentTask8 = new GanttTaskInfo(8, "Project1 task 8", new Date(2010, 6, 15), 100, 10, "");
        parentTask8.addChildTask(new GanttTaskInfo(55555, "Project1 task 55555", new Date(2010, 6, 20), 60, 10, ""));
        parentTask4.addChildTask(parentTask8);
        parentTask4.addChildTask(new GanttTaskInfo(9, "Project1 task 9", new Date(2010, 6, 15), 80, 50, ""));
        parentTask4.addChildTask(new GanttTaskInfo(10, "Project1 task 10", new Date(2010, 6, 16), 80, 10, ""));
        parentTask2.addChildTask(parentTask4);
        parentTask2.addChildTask(new GanttTaskInfo(11, "Project1 task 11", new Date(2010, 6, 21), 60, 100, "6"));
        parentTask2.addChildTask(new GanttTaskInfo(12, "Project1 task 12", new Date(2010, 6, 8), 110, 1, "14"));
        parentTask2.addChildTask(new GanttTaskInfo(14, "Project1 task 14", new Date(2010, 6, 7), 8, 10, ""));
        parentTask2.addChildTask(new GanttTaskInfo(15, "Project1 task 15", new Date(2010, 6, 9), 110, 90, "14"));
        var parentTask6 = new GanttTaskInfo(35, "Project1 task 35", new Date(2010, 7, 1), 130, 80, "1");
        parentTask6.addChildTask(new GanttTaskInfo(36, "Project1 task 36", new Date(2010, 7, 9), 64, 5, ""));
        parentTask6.addChildTask(new GanttTaskInfo(37, "Project1 task 37", null, 110, 90, ""));
        project1.addTask(parentTask1);
        project1.addTask(parentTask2);
        project1.addTask(parentTask6);
        //project 2
        var project2 = new GanttProjectInfo(2, "Project 2", new Date(2010, 5, 17));
        var parentTask22 = new GanttTaskInfo(62, "Project2 task 62", new Date(2010, 5, 17), 157, 50, "");
        parentTask22.addChildTask(new GanttTaskInfo(63, "Project2 task 63", new Date(2010, 5, 22), 78, 40, ""));
        parentTask22.addChildTask(new GanttTaskInfo(64, "Project2 task 64", null, 90, 90, ""));
        project2.addTask(parentTask22);
        var parentTask70 = new GanttTaskInfo(70, "Project2 task 70", new Date(2010, 5, 18), 120, 10, "");
        parentTask70.addChildTask(new GanttTaskInfo(71, "Project2 task 71", new Date(2010, 5, 18), 50, 0, ""));
        project2.addTask(parentTask70);
        //project 3
        var project3 = new GanttProjectInfo(3, "Project 3", new Date(2010, 5, 14));
        var parentTask80 = new GanttTaskInfo(80, "Project3 task 80", new Date(2010, 5, 14), 120, 10, "");
        project3.addTask(parentTask80);
        // Create Gantt control
        var ganttChartControl = new GanttChart();
        // Setup paths and behavior
        ganttChartControl.setImagePath("../../codebase/imgs/");
        ganttChartControl.showTreePanel(true);
        ganttChartControl.showContextMenu(true);
        ganttChartControl.showDescTask(true,'p,n,e,d');// p - percentCompleted,n - name,e - est, d - duration
        ganttChartControl.showDescProject(true,'n,s,d,p');// p - percentCompleted,n - name,s - start date, d - duration
        ganttChartControl.setEditable(true);
        ganttChartControl.showNewProject(true);
        ganttChartControl.setLoadPath("php/loadXML.php");
        ganttChartControl.setSavePath("php/saveXML.php");
        // Load data structure
        ganttChartControl.addProject(project1);
        ganttChartControl.addProject(project2);
        ganttChartControl.addProject(project3);
        // Build control on the page
        ganttChartControl.create(htmlDiv);
        // Define event handler for button "Save data"
        var b1 = document.getElementById("b1");
        b1.onclick = function()
        {
            var name = document.getElementById("t1").value;
            ganttChartControl.saveData(name);
        }
        // Define event handler for button "Load data"
        var b2 = document.getElementById("b2");
        b2.onclick = function()
        {
            var name = document.getElementById("t2").value;
            ganttChartControl.loadData(name,true,true);
        }

    }
    /*]]>*/
    </script>

    <div style="width:950px; height:620px; position:relative" id="GanttDiv"></div>
    <table>
      <tr>
    <td><input type="button" id="b1" value="Save data" /></td>
    <td><input type="text" id="t1" value="chart_name.xml" />
        To use save option, you should run this sample under Apache with PHP.</td>
    </tr>
    <tr>
       <td><input type="button" id="b2"  value="Load data"/></td>
       <td><select id="t2">
             <option value="data/data.xml">data/data.xml</option>
             <option value="data/data2.xml">data/data2.xml</option>
             <option value="data/data3.xml">data/data3.xml</option>
             <option value="data/sample1.xml">data/sample1.xml</option>
             <option value="data/sample2.xml">data/sample2.xml</option>
           </select></td>
       </tr>
    </table>