How to remove connectors from diagram if it do not gets connected to any node
This article explains how to remove connectors from a diagram if they do not gets connected to any node. In the diagram, the connectors are used to create a link between two points, ports, or nodes to represent the relationship between them. To connect the connector between two nodes, the connector must have a sourceID and targetID.
You can remove the connector from the diagram when the connector end is not connected to the node. The connectors’ sourceID and targetID properties are used to identify whether the connector is connected to a node or not. The following code shows, how to remove the connector when the connector is not connected to the node on each end.
// Remove the connector when it is not connected to the node
document.getElementById('remove').onclick = function () {
for (let i = 0; i < diagram.connectors.length; i++) {
let connector = diagram.connectors[i];
if (connector.sourceID === '' || connector.targetID === '') {
diagram.remove(connector);
--i;
}
}
};
//HTML
<div class="content-wrapper">
<input type="button" id="remove" value="remove" />
<div id="diagram"></div>
</div>
index.js
//Initialize the diagram
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '645px',
nodes: nodes,
connectors: connections,
});
diagram.appendTo('#diagram');In the above code sample, the onClick event is used to remove the connectors, which are not connected to any nodes. The onClick event will be triggered after the Remove button is clicked. In that event, all the connectors in the diagram are iterated to check whether the sourceID and targetID are present in the connector. If the connector is not connected to any node, then the sourceID and targetID will be set as "". If the sourceID and targetID are empty, the remove() method will remove that connector.
var connections = [
{
id: 'connector1',
sourceID: 'start',
targetID: 'submit',
},
{
id: 'connector2',
sourceID: 'submit',
targetID: 'approval',
},
{
id: 'connector3',
sourceID: 'approval',
targetID: 'reject',
},
{
id: 'connector4',
sourceID: 'approval',
targetID: 'submit2',
},
{
id: 'connector5',
sourceID: 'approval',
targetID: 'resubmit',
},
{
id: 'connector6',
sourcePoint: { x: 600, y: 100 },
targetPoint: { x: 800, y: 100 },
},
{
id: 'connector7',
sourceID: 'start',
targetPoint: { x: 800, y: 250 },
},
{
id: 'connector8',
sourcePoint: { x: 300, y: 200 },
targetID: 'submit',
},
];In the above code sample, the connector6 does not have sourceID and targetID, connector7 does not have targetID, and connector8 does not have sourceID. So, when iterating these connectors in the onClick event, these connectors will get removed. And the remaining connectors have both sourceID and targetID so, those connectors will not get removed while iterating.
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to remove connectors from a diagram if it does not gets connected to any node.
You can refer to our JavaScript Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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 from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out 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!