Javascript mxGraph codec.decode不向实际图形添加单元格
我正在尝试加载XML文件并将其转换为Javascript mxGraph codec.decode不向实际图形添加单元格,javascript,xml,mxgraph,Javascript,Xml,Mxgraph,我正在尝试加载XML文件并将其转换为mxGraph,但是当我调用编解码器时,它不会更新我的图形 这是我的函数,其中容器是我的图形所在的div: function loadXml(container, xml) { // Checks if the browser is supported if (!mxClient.isBrowserSupported()) { // Displays an error message if the browser is not s
mxGraph
,但是当我调用编解码器时,它不会更新我的图形
这是我的函数,其中容器是我的图形所在的div:
function loadXml(container, xml)
{
// Checks if the browser is supported
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// Creates the graph inside the given container
var graph = new mxGraph(container);
// Adds rubberband selection to the graph
new mxRubberband(graph);
var doc = mxUtils.parseXml(xml);
var codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
}
};
PS:我检查了
doc.documentElement
,它似乎是正确的。由于使用Webpack的exports loader导入mxGraph,我出现了同样的问题,但我相信在其他情况下可能会发生
mxCodec.prototype.decode(doc,graphModel)
函数期望通过窗口[objectName]
访问器在全局范围内提供所有mxGraph对象/构造函数
如果mxGraph封装在模块中,则对象不在全局范围内,解码失败
要确认这是问题所在,请在mxGraph的
mxCodec.prototype.decode
函数中添加断点并加载页面。然后,您可以验证是否可以找到对象。我知道这个问题很老了,但我将为社区提供答案。您可以使用mxUtils.parseXml(xml)
函数解析xml字符串并从中取出单元格,然后只需将单元格导入mxGraph
对象,代码如下所示:
// Creates the graph inside the given container
var graph = new mxGraph(container);
let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
let elt = doc.documentElement.firstChild;
let cells = [];
while (elt != null)
{
let cell = codec.decode(elt)
if(cell != undefined){
if(cell.geometry != undefined){
if(cell.id != undefined && cell.parent != undefined && (cell.id == cell.parent)){
elt = elt.nextSibling;
continue;
}
cells.push(cell);
}
}
elt = elt.nextSibling;
}
graph.addCells(cells);
这应该可以做到:)我也有同样的问题:(