Can we export a JsPlumb flowchart as a JSON or XML?

回眸只為那壹抹淺笑 提交于 2019-11-30 00:56:42

To export jsPlumb flowchart to JSON / XML firstly you would need to collect information about flowchart elements, then serialize it.

Enumerating Blocks

To get information about blocks you can use plain jQuery:

var blocks = []
$("#main .node").each(function (idx, elem) {
    var $elem = $(elem);
    blocks.push({
        blockId: $elem.attr('id'),
        positionX: parseInt($elem.css("left"), 10),
        positionY: parseInt($elem.css("top"), 10)
    });
});

Enumerating Connections

To get information about connections between elements you can use jsPlumb API, particularly "jsPlumb.getConnections()" method:

var connections = [];
$.each(jsPlumb.getConnections(), function (idx, connection) {
    connections.push({
        connectionId: connection.id,
        pageSourceId: connection.sourceId,
        pageTargetId: connection.targetId
    });
});

Serializing to JSON

Once you collected all flowchart data you can serialize it to JSON:

var serializedData = JSON.stringify(blocks);

We can use GetAllConnections instead of getConnection.

var connections = [];
function GetConn() {      
//connections = jsPlumb.getAllConnections();

$.each(jsPlumb.getAllConnections(), function (idx, connection) {
connections.push({
connectionId: connection.id,
pageSourceId: connection.sourceId,
pageTargetId: connection.targetId,
sourceText: connection.source.innerText,
targetText: connection.target.innerText,
});
});    
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!