How to Add Phase at Runtime in JavaScript Diagram?
This article explains how to add a phase at runtime in JavaScript Diagram. In Syncfusion® JavaScript Diagram, you can dynamically add phases to a diagram at runtime using a context menu.
- This feature enhances user interaction by allowing users to insert phrases directly into the swimlane.
- To add a phase using the context menu, you can use the
addPhasesmethod. In theaddPhasesmethod, you pass the selected swimlane and new phase data as arguments.
In the following code, demonstrates how to add phase using context menu:
contextMenuClick: function (args) {
if ((args.item.id === 'InsertPhaseBefore' || args.item.id === 'InsertPhaseAfter') &&
(diagram.selectedItems.nodes[0]).isPhase) {
// The currently selected phase
let node = diagram.selectedItems.nodes[0];
let swimlane = diagram.getObject((diagram.selectedItems.nodes[0]).parentId);
let shape = swimlane.shape;
let existingPhase = shape.phases[node.columnIndex];
// Create a new phase object to be added
let newPhase = {
id: ej.diagrams.randomId(),
offset: existingPhase.offset,
header: { style: existingPhase.header.style },
style: existingPhase.style,
};
// To add a phase before the existing phase
if (args.item.id === 'InsertPhaseBefore') {
newPhase.offset = newPhase.offset - newPhase.offset / 2;
diagram.addPhases(swimlane, [newPhase]);
}
else {
// To add a phase after the existing phase
newPhase.offset = newPhase.offset + newPhase.offset / 2;
if (newPhase.offset >= swimlane.width) {
newPhase.offset = existingPhase.offset - 50;
}
diagram.addPhases(swimlane, [newPhase]);
}
}
}
Refer to the working sample for additional details and implementation: Click here for sample
Conclusion
We hope you enjoyed learning about how to add a phase at runtime in the JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, as well as how to quickly get started with configuration specifications.
You can also explore our JavaScript Diagram example to understand how to create and manipulate data. For current customers, you can check out our components on the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!