New row position in EJ2 TypeScript Gantt control
2 May 20236 minutes to read
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.
import { ContextMenu, MenuItemModel, ContextMenuModel } from '@syncfusion/ej2-navigations';
import { Gantt, Toolbar, Filter, Edit, Selection} 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, Selection, Filter, Edit);
enableRipple(true);
let menuItems: MenuItemModel[] = [
{
text: 'Top'
},
{
text: 'Bottom'
},
{
text: 'Above'
},
{
text: 'Below'
},
{
text: 'Child'
},];
let menuOptions: ContextMenuModel = {
items: menuItems,
select: select
};
let menuObj: ContextMenu = new ContextMenu(menuOptions, '#contextmenu');
let clickHandler: EmitType<ClickEventArgs> = (args: ClickEventArgs) => {
if (args.item.text === 'Add') {
menuObj.open(60, 20);
}
};
function select(args: any) {
let gantt: any = (document.getElementsByClassName('e-gantt')[0] as any).ej2_instances[0];
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();
}
}
let gantt: Gantt = new Gantt({
dataSource: GanttData,
height: '450px',
allowSelection: true,
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' }],
});
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>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='Gantt'></div>
<ul id="contextmenu"></ul>
</div>
</body>
</html>