How to create layout with HTML nodes at runtime in JavaScript Diagram?
Using Syncfusion® JavaScript Diagram, you can create automatic layouts that visually represent the structure of an organization and its relationships. In this knowledge base article, we will explain how to dynamically create layouts with HTML nodes using the event handling mechanisms provided by Syncfusion®. For instance, you can utilize the created
event to dynamically render a layout.
To implement an automatic layout, define the layout property of the diagram and specify the desired layout type. To add an assistant to the organizational chart, use the getLayoutInfo
function. Here’s a code snippet demonstrating this:
created: function () {
diagram.layout = {
type: 'OrganizationalChart',
getLayoutInfo: (node, options) => {
if (node.data['Role'] === 'General Manager') {
options.assistants.push(options.children[0]);
options.children.splice(0, 1);
}
if (!options.hasSubTree) {
options.type = 'Right';
}
},
};
diagram.doLayout();
},
To render the layout, it’s essential to assign the JSON data to the dataSource property within the dataSourceSettings object of the diagram.
created: function () {
diagram.dataSourceSettings = {
id: 'Id',
parentId: 'Manager',
//you need to set your data from db to dataManager
dataSource: new ej.data.DataManager(localBindData),
doBinding: (nodeModel, data, diagram) => {
nodeModel.shape = {
type: 'HTML',
};
},
};
},
We have created the layout using the HTML node. When creating HTML nodes dynamically, you define a nodeTemplate that specifies the structure and appearance of each node. Here’s an example of how you might define a nodeTemplate for HTML nodes:
<script id="nodeTemplate" type="text/x-template">
<div style="width: 16px; height: 16px; background-color: ${data.color}; border-radius: 16px;">
<div class="details">
<p class="myTitle"> ${data.Role}</p>
<p>Description</p>
<button
>${data.Role}</button>
</div>
</div>
</script>
Conclusion
I hope you enjoyed learning about how to create layout with HTML nodes at runtime in JavaScript Diagram.
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!