DHTMLX Docs & Samples Explorer

Custom menu in Gantt Chart

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>
<link rel="stylesheet" type="text/css" href="../../../dhtmlxMenu/codebase/skins/dhtmlxmenu_dhx_skyblue.css">
<script type="text/javascript" language="JavaScript" src="../../../dhtmlxMenu/codebase/dhtmlxmenu.js"></script>
<script type="text/javascript" language="JavaScript" src="../../../dhtmlxMenu/codebase/ext/dhtmlxmenu_ext.js"></script>
<script type="text/javascript" language="JavaScript">
/*<![CDATA[*/
var ganttChartControl;
function createChartControl(htmlDiv1)
{
    // Initialize Gantt data structures
    //project 1
    var project1 = new GanttProjectInfo(1, "Applet redesign", new Date(2010, 5, 11));
    var parentTask1 = new GanttTaskInfo(1, "Old code review", new Date(2010, 5, 11), 208, 50, "");
    parentTask1.addChildTask(new GanttTaskInfo(2, "Convert to J#", new Date(2010, 5, 11), 100, 40, ""));
    parentTask1.addChildTask(new GanttTaskInfo(13, "Add new functions", new Date(2010, 5, 12), 80, 90, ""));
    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, "");
    var parentTask123 = new GanttTaskInfo(123, "use GUIDs", new Date(2010, 6, 14), 60, 70, "");
    parentTask5.addChildTask(parentTask123);
    parentTask2.addChildTask(parentTask5);
    project1.addTask(parentTask1);
    project1.addTask(parentTask2);
    // Create Gantt control
    ganttChartControl = new GanttChart();
    // Setup paths and behavior
    ganttChartControl.setImagePath("../../codebase/imgs/");
    ganttChartControl.setEditable(true);
    ganttChartControl.showTreePanel(true);
    ganttChartControl.showDescTask(true,'s-f');
 
    // Sample custom menu
    var menu = new dhtmlXMenuObject();
    menu.setIconsPath("../../../dhtmlxMenu/codebase/imgs/dhtmlxmenu_dhx_skyblue");
    menu.renderAsContextMenu();
    menu.loadXMLString('<menu><item id="m1" text="Item name placeholder"/><item id="m2" text="Get info"/><item id="m3" text="Delete Task"/></menu>');
    menu.attachEvent("onClick", function(id){
        var obj = menu.getUserData("","obj");
        if (obj) {
            var o = obj.o;
            if (obj.type == "p") {
                alert("Project, id=" + o.getId() + ", name=" + o.getName() + ", start date=" + o.getStartDate() +
                        ", duration=" + o.getDuration() + "hours, percent complete=" + o.getPercentCompleted() + "%");
            } else
            if (obj.type == "t") {
                if(id=="m3") {
                    // Handle "Delete Task" menu
                    if (confirm("Delete task \""+o.getName()+"\"?")) ganttChartControl.getProjectById(1).deleteTask(o.getId());
                } else
                    alert("Task, id=" + obj.o.getId() + ", name=" + obj.o.getName() + ", EST=" + obj.o.getEST() +
                          ", duration=" + o.getDuration() + "hours, percent complete=" + o.getPercentCompleted() + "%" +
                          ", parentTaskId=" + o.getParentTaskId() + ", pred.taskId=" + o.getPredecessorTaskId());
            }
        }
    });
    ganttChartControl.setContextMenu(menu);
    ganttChartControl.attachEvent("onBeforeContextMenu", function(menu,obj) {
        // Sample of disabling menu for a particular item with Id = 13
        if (obj.getId()==13) {alert("This task has no menu."); return false;}
        if (obj.isProject) {
            // Project menu
            menu.setItemText("m1", "Project: " + obj.getName());
            menu.setUserData("","obj",{type:"p",o:obj});
            menu.setItemDisabled("m3");
        } else if (obj.isTask) {
            // Task menu
            menu.setItemText("m1", "Task: " + obj.getName());
            menu.setUserData("","obj",{type:"t",o:obj});
            menu.setItemEnabled("m3");
        }
    });
 
    
    // Load data structure        
    ganttChartControl.addProject(project1);
    // Build control on the page
    ganttChartControl.create(htmlDiv1);
}
/*]]>*/
</script>
 
 
<div style="width:950px; height:500px; position:relative;" id="GanttDiv"></div>