Drag and Drop the Record from another component to Gantt

17 Feb 20224 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.

<ejs-gantt id='Gantt' dataSource="ViewBag.DataSource" height="280px" gridLines="Both" treeColumnIndex="1"
           resources="ViewBag.projectResources">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" endDate="EndDate" duration="Duration"
                        resourceInfo="Resources" progress="Progress" dependency="Predecessor" child="SubTasks">
    </e-gantt-taskfields>
    <e-gantt-editsettings allowEditing="true"></e-gantt-editsettings>
    <e-gantt-resourcefields id="ResourceId" name="ResourceName"></e-gantt-resourcefields>
    <e-gantt-labelSettings rightLabel="${if(Resources)}${Resources}${/if}"></e-gantt-labelSettings>
</ejs-gantt>
<ejs-treeview id="treedata" allowDragAndDrop="true" nodeDragStop="nodeDragStop">
    <e-treeview-fields dataSource="ViewBag.projectResources" id="ResourceId" text="ResourceName"></e-treeview-fields>
</ejs-treeview>
<script>
    function nodeDragStop(args) {
        var ganttChart = document.getElementById('Gantt').ej2_instances[0];
        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.ariaRowIndex;
           ganttChart.selectRow(Number(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 elements = document.querySelectorAll('.e-drag-item');
            while (elements.length > 0 && elements[0].parentNode) {
                elements[0].parentNode.removeChild(elements[0]);
            }
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    ViewBag.projectResources = projectResources();
    return View();
}

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