如何从Java/Prime Faces将JSON对象传递到GOJS javascript文件
我有一个java类,它返回一个JSON对象,我需要将这个JSON对象传递给GOJSJavaScript,下面是我的GOJSJavaScript文件(gojs_org_view.js)的示例 //一些javascript代码如何从Java/Prime Faces将JSON对象传递到GOJS javascript文件,javascript,jquery,json,primefaces,gojs,Javascript,Jquery,Json,Primefaces,Gojs,我有一个java类,它返回一个JSON对象,我需要将这个JSON对象传递给GOJSJavaScript,下面是我的GOJSJavaScript文件(gojs_org_view.js)的示例 //一些javascript代码 function save() { var json = JSON.parse(myDiagram.model.toJson()); document.getElementById("mySavedModel").value = JSON.stringify(js
function save() {
var json = JSON.parse(myDiagram.model.toJson());
document.getElementById("mySavedModel").value = JSON.stringify(json.nodeDataArray, null, ' ');
}
function load() {
var nodeDataArray =
[ {"id":"1", "pid":"1", "name":"Corrado 'Junior' Soprano", "title":"The Boss", "email":"boss@sopranos.net"},
{"id":"2", "pid":"1", "name":"Tony Soprano", "title":"Underboss", "email":"underboss@sopranos.net"},
{"id":"3", "pid":"1", "name":"Herman 'Hesh' Rabkin", "title":"Advisor", "email":"hesh@sopranos.net"},
{"id":"4", "pid":"2", "name":"Paulie Walnuts", "title":"Capo", "email":"paulie@sopranos.net"},
{"id":"5", "pid":"2", "name":"Ralph Cifaretto", "title":"Capo MIA", "email":"ralph@sopranos.net"},
{"id":"6", "pid":"2", "name":"Silvio Dante", "title":"Consigliere", "email":"silvio@sopranos.net"}];
var model = new go.TreeModel();
model.nodeKeyProperty = "id";
model.nodeParentKeyProperty = "pid";
model.nodeDataArray = nodeDataArray;
myDiagram.model = model;
myDiagram.undoManager.isEnabled = true;
}
function onSelectionChanged(e) {
var node = e.diagram.selection.first();
if (node instanceof go.Node) {
updateProperties(node.data);
} else {
updateProperties(null);
}
}
// Update the HTML elements for editing the properties of the currently selected node, if any
function updateProperties(data) {
if (data === null) {
jQuery("#selected").css('visibility', 'hidden');
jQuery("#name").html("");
jQuery("#title").html("");
jQuery("#email").html("");
jQuery("#site").html("");
jQuery("#superior").html("");
} else {
jQuery("#selected").css('display', 'block');
jQuery("#name").html("ID: " + data.id + " Name: " + data.fullname || "");
jQuery("#title").html("Title: " + data.title || "");
jQuery("#email").html("Email: " + data.email || "");
jQuery("#site").html("Site: " + data.siteName || "");
jQuery("#superior").html("Superior: " + data.pname || "");
}
}
javascript变量unasocArray和nodeDataArray是硬编码的,而这些值必须来自java类或xhtml(我不确定哪种方法是将JSON对象发送到gojs javascript文件的最佳方法,因为我不熟悉prime faces和JSON)。
下面是调用gojs的xhtml代码
<p:panel id="relationshipsPanel">
<h:outputScript library="js" name="go.js" />
<h:outputScript library="js" name="gojs_org_view.js" />
<h:outputStylesheet library="css" name="gojs_org_view.css"/>
<div id="sample">
<div id="myPalette" class="myPaletteClass"></div>
<div id="myDiagram" class="myDiagramClass"></div>
<div id="myOverview" class="inner"></div>
<div id="selected" class="selectedClass"></div>
</div>
</p:panel>
我尝试了下面的隐藏值,我在视图中添加了下面的行
在gojs_org_view.js中,我做了window.console.log(“RANA ORG JSON----------------------------------------->”+document.getElementById(“relationshipsForm:arrVal”).value); var nodeDataArray=JSON.parse(document.getElementById(“relationshipsForm:arrVal”).value)
在控制台中,我从javascript的视图中获取以下JSON对象,但在var nodeDataArray=JSON.parse(document.getElementById(“relationshipsForm:arrVal”).value处抛出错误“Uncaught SyntaxError:Unexpected token i” 您可以使用AJAX调用从服务器请求数据并获取数据。如果这不是一个选项,那么您可以将数据放在页面上的隐藏字段中,然后在
load()
函数中访问它,就像在save()
函数中使用document.getElementById
一样 尝试使用验证json,看起来json中有错误
<p:panel id="relationshipsPanel">
<h:outputScript library="js" name="go.js" />
<h:outputScript library="js" name="gojs_org_view.js" />
<h:outputStylesheet library="css" name="gojs_org_view.css"/>
<div id="sample">
<div id="myPalette" class="myPaletteClass"></div>
<div id="myDiagram" class="myDiagramClass"></div>
<div id="myOverview" class="inner"></div>
<div id="selected" class="selectedClass"></div>
</div>
</p:panel>