Drag and drop in EJ2 JavaScript Gantt control

23 Jun 20237 minutes to read

In Gantt, it is possible to drag a record from another component and drop it in Gantt chart with updating the Gantt record. Here, dragging an item from TreeView component to Gantt and that item is updated as a resource for the Gantt record, we can achieve this, by using nodeDragStop event of TreeView control.

ej.gantt.Gantt.Inject(ej.gantt.Edit);
var treeObj = new ej.navigations.TreeView({
    fields: { dataSource: editingResources, id: 'resourceId', text: 'resourceName' },
    allowDragAndDrop: true,
    height: "200px",
    nodeDragStop: function (args) {
        args.cancel = true;
        var chartEle = ej.base.closest(args.target, '.e-chart-row');
        var gridEle = ej.base.closest(args.target, '.e-row');
        if(gridEle) {
          var index = ganttChart.treeGrid.getRows().indexOf(gridEle);
          ganttChart.selectRow(index);
        }
        if (chartEle) {
            var index = chartEle.rowIndex;
            ganttChart.selectRow(index);
        }
        var record= args.draggedNodeData;
        var selectedData = ganttChart.flatData[ganttChart.selectedRowIndex];
        var selectedDataResource = selectedData.taskData.resources;
        var resources = [];
        if (selectedDataResource) {
            for (var i = 0; i < selectedDataResource.length; i++) {
              resources.push(selectedDataResource[i].resourceId);
            }
        }
        resources.push(parseInt(record.id));
        if (chartEle || gridEle) {
            var data = {
                TaskID: selectedData.taskData.TaskID,
                resources: resources
            };
            ganttChart.updateRecordByID(data);    
        }
    }
});
var ganttChart = new ej.gantt.Gantt({
        dataSource: editingData,
        resources: editingResources,
		height:'450px',
		taskFields: {
            id: 'TaskID',
            name: 'TaskName',
            startDate: 'StartDate',
			duration: 'Duration',
            progress: 'Progress',
			parentID: 'ParentID',
            dependency: 'Predecessor',
            resourceInfo: 'resources'
        },
        editSettings: {
           allowEditing: true
        },
        resourceFields: {
            id: 'resourceId',
            name: 'resourceName'
        },
		splitterSettings:{
		  position: "30%"
		},
        labelSettings: {
            rightLabel: 'resources'
        }
});
ganttChart.appendTo('#Gantt');
treeObj.appendTo('#TreeView');
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>EJ2 Gantt</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Typescript Gantt Controls">
        <meta name="author" content="Syncfusion">
        <link href="index.css" rel="stylesheet">
        <link href="https://cdn.syncfusion.com/ej2/32.1.19/material.css" rel="stylesheet" type="text/css">
        <script src="https://cdn.syncfusion.com/ej2/32.1.19/dist/ej2.min.js" type="text/javascript"></script>
        <script src="es5-datasource.js" type="text/javascript"></script>
    <script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

    <body>
        <div id="container">
            <b><label>Gantt</label></b>
            <div id="Gantt"></div>
            <b><label>List</label></b>
            <div id="TreeView" style="width: 500px;"></div>
        </div>
        <script>
            var ele = document.getElementById('container');
            if(ele) {
            ele.style.visibility = "visible";
            }   
        </script>
        <script src="index.js" type="text/javascript"></script>
    </body>
</html>

The following screenshot shows dropping record from another component in to Gantt, and Rose Fuller is added as resource for the task Develop floor plan estimation.

Dropping Record