How to expand children of the node using userhandle in Angular Diagram Layout?
This article explains how to expand children of a node using user handle in the Angular Diagram layout. In the Angular Diagram layout, we can expand and collapse a large number of nodes with the expand and collapse feature. Additionally, we can achieve the expansion and collapse of the child 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 diagram
<ejs-diagram #diagram id="diagram" width="100%" height="700px" [getNodeDefaults]='nodeDefaults' [layout]='layout' [dataSourceSettings]='data' [selectedItems]='selectedItems' (onUserHandleMouseDown)='onUserHandleMouseDown()'>
</ejs-diagram>
//initialise handle
public 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 },
}];
public onUserHandleMouseDown(args) {
let selectedNode = this.diagram.selectedItems.nodes[0];
let id = (selectedNode.data as any).Id;
this.renderLayout(id);
}
public selectedItems = {
constraints: SelectorConstraints.All | SelectorConstraints.UserHandle,
userHandles: this.handle as any,
}
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 the dataBind method.
//method to render child nodes
public renderLayout(id: string) {
let datasource = this.diagram.dataSourceSettings.dataSource.dataSource.json;
let filteredData = this.diagramData.filter((item) => (item as any).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 as any).Id]);
Array.prototype.push.apply(datasource, filteredData);
this.diagram.dataSourceSettings.dataSource = new DataManager(datasource);
this.diagram.dataBind();
}
This code facilitates the dynamic expansion and collapse of nodes in a diagram based on user interaction with user handles, using a filtering mechanism to adjust the data source accordingly.
Refer to the working sample for additional details and implementation : Sample
Conclusion
We hope you enjoyed learning about how to expand the children of a node using userHandle in Angular Diagram Layout.
You can refer to our Angular 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 Angular 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!