How to expand children of the node using userhandle in React Diagram Layout?
This article explains how to expand the children of a node using a user handle in React Diagram Layout. In the React 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
<DiagramComponent id="diagram" ref={diagram => (diagramInstance = diagram)} width={"100%"} height={"700px"}
dataSourceSettings={{dataSourceSettings}} layout={{layout}}
selectedItems={{
constraints: SelectorConstraints.All | SelectorConstraints.UserHandle,
userHandles: handle,
}}
onUserHandleMouseDown={(args) => {
let selectedNode = diagramInstance.selectedItems.nodes[0];
let id = (selectedNode.data).Id;
renderLayout(id);
}
}>
<Inject services={[DataBinding, HierarchicalTree, LayoutAnimation]} />
</DiagramComponent>
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 = diagramInstance.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.Id] = true;
});
filteredData = filteredData.filter((item) => !idLookup[item.Id]);
Array.prototype.push.apply(datasource, filteredData);
diagramInstance.dataSourceSettings.dataSource = new DataManager(datasource);
diagramInstance.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 a user handle in the React Diagram Layout.
You can refer to our React Diagram feature tour page to learn about its other groundbreaking feature representations and documentation. You can also explore our React Diagram example to understand how to present 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 React Diagram and other components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!