How to save properties for custom components to JSON?
In the diagram, you can save properties for custom components to JSON. You need to save the diagram using the diagram API saveDiagram and send a JSON string from the client-side to the server-side and save it into the database. Similarly, you can retrieve the same saved JSON from the database and send it to the client-side from the server-side and load it in the diagram using the diagram loadDiagram method.
Please refer to the following code example to save properties for custom components to JSON.
JS
function saveDiagramJson() {
var diagram = document.getElementById("diagram");
diagram = diagram.ej2_instances[0];
//Initializes the saveDiagram API
var data = diagram.saveDiagram();
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:57270/api/Entity/SaveData');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 500) {
alert('success');
}
}
//sends requests to save the data
xhr.send(JSON.stringify(data));
};
Sample: https://ztff9a-vjppyr.stackblitz.io
Refer to the following code example for how the saveDiagram API works.
[AcceptVerbs("GET", "POST")]
[HttpPost] //String converted into JSON data.
public IHttpActionResult SaveData([FromBody]string JSONData)
{
DiagramEntities2 entity = new DiagramEntities2();
SaveLoadData data = new SaveLoadData();
data.Name = "diagram";
data.SavedData = JSONData;
entity.SaveLoadDatas.Add(data);
entity.SaveChanges();
return null;
}
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Webapisample-310135299
Conclusion
I hope you enjoyed learning about how to save properties for custom components to JSON.
You can refer to our 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!