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.
var clickHandler = function(args){
if (args.item.id === 'GanttExport_add') {
var contextMenuObj = document.getElementById("contextmenu").ej2_instances[0];
contextMenuObj.open(60, 20);
}
};
ej.gantt.Gantt.Inject(ej.gantt.Toolbar);
var menuItems = [
{
text: 'Bottom'
},
{
text: 'Above'
},
{
text: 'Below'
},
{
text: 'Child'
},
{
text: 'Top'
}];
var menuOptions = {
items: menuItems,
select: select
};
var menuObj = new ej.navigations.ContextMenu(menuOptions, '#contextmenu');
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();
}
}
var ganttChart = new ej.gantt.Gantt({
dataSource: GanttData,
height:'450px',
allowSelection: true,
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
editSettings:{
allowAdding:true
},
toolbar: ['Edit', { text: 'Add', tooltipText: 'Add', id: 'Add' }],
toolbarClick: clickHandler,
});
ganttChart.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/21.2.3/material.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="Gantt"></div>
<ul id="contextmenu"></ul>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>