How to expand children of a node using user handle in JavaScript Diagram layout?
In the JavaScript Diagram layout, we can expand and collapse a large number of nodes with the expand and collapse feature. Additionally, we can achieve the expand and collapse of the children nodes by clicking on user handles. To achieve this, we utilize a method to filter the parent ID from the data source and dynamically modify the diagram’s data source.
//initialise handle
let handle = [
{
name: 'child',
tooltip: { content: 'Add child' },
pathData:
'M6.09,0 L13.75,6.88 L6.09,13.75 L6.09,9.64 L0,9.64 L0,4.11 L6.09,4.11 Z ',
visible: true,
offset: 0.5,
side: 'Bottom',
margin: { top: 0, bottom: 0, left: 0, right: 0 },
},
];
let diagram: Diagram = new Diagram({
selectedItems: {
constraints: SelectorConstraints.All | SelectorConstraints.UserHandle,
userHandles: handle as any,
},
onUserHandleMouseDown: function (args) {
let selectedNode = diagram.selectedItems.nodes[0];
let id = (selectedNode.data as any).Id;
render(id);
},
});
diagram.appendTo('#diagram');
The filter function takes an ID as a parameter and filters the data source dynamically based on the ID. It filters the data to include only those nodes whose ‘Manager’ property matches the provided ID. It utilizes a lookup object (idLookup) to store existing IDs and ensures that duplicate IDs are not added to the data source. The filtered data is then appended to the existing data source of the diagram, and the diagram is updated using dataBind method.
//method to render child nodes
function render(id: string) {
let datasource = diagram.dataSourceSettings.dataSource.dataSource.json;
let filteredData = diagramData.filter((item) => item.Manager === id);
// Lookup object to store existing IDs
const idLookup = {};
datasource.forEach((item) => {
idLookup[(item as any).Id] = true;
});
filteredData = filteredData.filter((item) => !idLookup[item.Id]);
Array.prototype.push.apply(datasource, filteredData);
diagram.dataSourceSettings.dataSource = new DataManager(datasource);
diagram.dataBind();
}
Refer the Sample for Reference
Conclusion
I hope you enjoyed learning about how to expand children of a node using user handle in JavaScript Diagram layout.
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, Direct-Trac, or feedback portal. We are always happy to assist you!