【问题标题】:how to dynamically add edges from xml without losing the source or target如何在不丢失源或目标的情况下从 xml 动态添加边
【发布时间】:2013-10-16 06:56:53
【问题描述】:

我正在尝试从 xml 文件构建图形,但仅加载基于特定条件的节点...我可以使用包含解码和添加功能的 for 循环添加所有顶点,

问题出在边缘 .. 我可以让它们拥有来源,但目标总是以 null 结束

在我尝试过的许多事情中,最终版本将 xml 节点添加到数组中,并且只有在将所有顶点添加到图形后,我才对边进行解码......但这仍然不起作用......

    function get_selected(main_class, filename){ 
    "use strict";
    var node, i, j, req, root, decj, deci, enc, dec, data, parent, graph, main_class_node, child_nodes, edges, destinations;
    deci = new mxCodec();
    decj = new mxCodec();
    graph = new mxGraph();
    parent = graph.getDefaultParent();

    req = mxUtils.load(filename);
    root = req.getDocumentElement();

    child_nodes = [];
    edges = [];
    destinations = [];

    mxLog.show();
    for (i=0; i < root.childNodes[0].childNodes.length; i ++) { 
        // Get the main cell and add it to the graph
        if (root.childNodes[0].childNodes[i].id == main_class) {
            node = deci.decode(root.childNodes[0].childNodes[i]);
            main_class_node = graph.getModel().add(parent, node);
        }
        //get all the children of the main cell and add them to the graph
        else if (root.childNodes[0].childNodes[i].outerHTML.indexOf('parent="'  + main_class + '"') != -1 ) {
            node = decj.decode(root.childNodes[0].childNodes[i]);
            graph.getModel().add(main_class_node, node);
            child_nodes.push(node.id);

        }
    }
    for (j=0; j < child_nodes.length; j ++) {
        for (i=0; i < root.childNodes[0].childNodes.length; i ++) { 
            //get all the edges originating in any of the "chidren" and add them to the graph 
            if (root.childNodes[0].childNodes[i].outerHTML.indexOf('source="' + child_nodes[j] + '"') != -1 )  {
                node = root.childNodes[0].childNodes[i];
                //graph.getModel().add(parent, node);
                edges.push(node);
            }
        }
    }
    for (j=0; j < edges.length; j ++) {
        for (i=0; i < root.childNodes[0].childNodes.length; i ++) { 
            //get the target of each of the edges and and add them to the graph
            if (root.childNodes[0].childNodes[i].id  ==  "attr-" + edges[j].id.split('-')[1]) {
                node = deci.decode(root.childNodes[0].childNodes[i]);
                graph.getModel().add(parent, node);
            }
        }
    }
    dec = new mxCodec();

    for (j=0; j < edges.length; j ++) {
        node = dec.decode(edges[j]);
        graph.getModel().add(parent, node);
    }

    enc = new mxCodec();
    data = enc.encode(graph.getModel());
    return mxUtils.getXml(data);
}

【问题讨论】:

    标签: mxgraph


    【解决方案1】:

    我的问题是有多个 mxCodecs ...我已经对代码进行了很多更改,但这是基本问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多