Search results

Add new row position while adding in JavaScript Gantt control

In Gantt, a new row can be added in one of the following positions: Top, Bottom, Above, Below and Child. This position can be specified through the newRowPostion property. We can make use of the toolbarClick event to create a context menu that specifies the position in which the new row is to be added when adding a record through toolbar click.

The following code snippets demonstrate how to achieve this.

Source
Preview
index.ts
index.html
import { ContextMenu, MenuItemModel, ContextMenuModel } from '@syncfusion/ej2-navigations';
import { Gantt, Toolbar, Filter } from '@syncfusion/ej2-gantt';
import { EmitType } from '@syncfusion/ej2-base';
import { ClickEventArgs } from '@syncfusion/ej2-navigations';
import { GanttData } from 'datasource.ts';
import { enableRipple } from '@syncfusion/ej2-base';

Gantt.Inject(Toolbar, Filter);

enableRipple(true);

let menuItems: MenuItemModel[] = [
    {
        text: 'Top'
    },
    {
        text: 'Bottom'
    },
    {
        text: 'Above'
    },
    {
        text: 'Below'
    },
    {
        text: 'Child'
    },];

let menuOptions: ContextMenuModel = {
    items: menuItems,
    select: itemSelect(menuItems);
};

let menuObj: ContextMenu = new ContextMenu(menuOptions, '#contextmenu');

let clickHandler: EmitType<ClickEventArgs> = (args: ClickEventArgs) => {
    if (args.item.text === 'Add') {
        menuObj.open(60, 20);
    }
};

let gantt: Gantt = new Gantt({
    dataSource: GanttData,
    height: '450px',
    taskFields: {
        id: 'TaskID',
        name: 'TaskName',
        startDate: 'StartDate',
        duration: 'Duration',
        progress: 'Progress',
        child: 'subtasks'
    },
    editSettings: {
        allowEditing: true,
        allowAdding: true,
        allowDeleting: true
    },
    toolbarClick: clickHandler,
    toolbar: [ 'Edit', { text: 'Add', tooltipText: 'Add', id: 'Add' }],
    actionBegin: function(args) {
        if (args.item.text === "Bottom") {
            gantt.editSettings.newRowPosition = "Bottom";
            gantt.openAddDialog();
        } else if (args.item.text === "Above") {
            if (gantt.selectedRowIndex == -1) {
                alert("Please select any row");
            } else {
                gantt.editSettings.newRowPosition = "Above";
                gantt.openAddDialog();
            }
        } else if (args.item.text === "Below") {
            if (gantt.selectedRowIndex == -1) {
                alert("Please select any row");
            } else {
                gantt.editSettings.newRowPosition = "Below";
                gantt.openAddDialog();
            }
        } else if (args.item.text === "Child") {
            if (gantt.selectedRowIndex == -1) {
                alert("Please select any row");
            } else {
                gantt.editSettings.newRowPosition = "Child";
                gantt.openAddDialog();
            }
        } else if (args.item.text === "Top") {
            gantt.editSettings.newRowPosition = "Top";
            gantt.openAddDialog();
        }
    }
});

gantt.appendTo('#Gantt');
<!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/material.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='Gantt'></div>        
    </div>
</body>

</html>