Copy paste records in EJ2 JavaScript Gantt control
2 May 20236 minutes to read
You can copy and paste a record in the Gantt chart by using the addRecord
method and custom context menu
. It is also possible to copy and paste the parent record with multiple hierarchical child records on the required position.
ej.base.enableRipple(true);
var copiedRecord;
var enableFlag;
var gantt = new ej.gantt.Gantt({
dataSource: GanttData,
height:'450px',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
dependency: 'Predecessor',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
editSettings: {
allowAdding: true,
allowEditing: true,
allowDeleting: true,
allowTaskbarEditing: true
},
toolbar: ['Add', 'Edit', 'Update', 'Delete', 'Cancel', 'ExpandAll', 'CollapseAll'],
allowSelection: true,
enableContextMenu: true,
contextMenuItems: [
{ text: 'Copy', target: '.e-content', id: 'copy' },
{ text: 'Paste', target: '.e-content', id: 'paste' },
],
contextMenuClick: function (args) {
if (args.item.id === 'copy') {
copiedRecord = args.rowData;
copiedRecord.taskData.TaskID = gantt.currentViewData.length + 1;
}
if (args.item.id === 'paste') {
gantt.addRecord(copiedRecord.taskData,'Below',args.rowData.index);
if(copiedRecord.hasChildRecords) {
addChildRecords(copiedRecord, args.rowData.index + 1);
}
copiedRecord = undefined;
}
},
contextMenuOpen: function (args) {
if (args.type !== 'Header') {
if (copiedRecord) {
args.hideItems.push('Copy');
} else {
args.hideItems.push('Paste');
}
}
}
});
gantt.appendTo('#Gantt');
function addChildRecords(record, index) {
for(var i=0; i<record.childRecords.length; i++) {
var childRecord = record.childRecords[i];
childRecord.taskData.TaskID = ganttChart.currentViewData.length + 1;
ganttChart.addRecord(childRecord.taskData,'Child',index);
if(childRecord.hasChildRecords) {
addChildRecords(childRecord, index + (i+1));
}
}
}
<!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://cdn.syncfusion.com/ej2/23.1.36/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">
<div id="Gantt"></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>