希望这能帮助和抱歉,我没有使用JSON。我将jstree与函数一起使用,通过单击jstree html外部的元素来打开节点。
我们设置中的每个节点都像一个网页,所以在仪表板的主页上我们有最近编辑过的页面列表。
每这些链接的
有这个JavaScript的执行
<a href="javascript: selectLeftNavNode(339);">Edit</a>
,其中339是我们要编辑
页的ID,这是用来执行
function selectLeftNavNode(node) {
$('#demo').jstree('deselect_all');
$('#demo').jstree('select_node', '#node_' + node);
}
FHE功能
我们最近注意到的问题是,如果最近编辑的页面在树中更深处,更具体地在尚未加载的段中,则将失败
这就是为什么我决定把AJAX请求到服务器,以便检索所有父ID的
改下面的代码,在我的情况下,阿贾克斯将返回这样的事情找XML
<?xml version="1.0" encoding="UTF-8"?>
<response>
<paths>
<path>339</path>
<path>338</path>
<path>38</path>
</paths>
</response>
和功能
function selectLeftNavNode(node) {
$('#demo').jstree('deselect_all');
if($('#demo').jstree('select_node', '#node_' + node) === false)
{
// if it is false means that the node is not yet rendered
// so instead of loading we will require to get list of parent nodes to open in order, then it will become available
// an ajax call should get us all the nodes
$.ajax({
type: "POST",
dataType: "xml",
url: your_url_to_php_script',
data: {node_id:node},
success: function(data)
{
var remaining_nodes = new Array();
var paths_count = $(data).find('response').find('path').length;
for(var x=1;x<=paths_count;x++){
remaining_nodes[x-1] = $(data).find('response').find('paths path:nth-child('+x+')').text();
}
open_nodes_step_by_step(remaining_nodes);
}
});
}
}
和在除了其通过环绕在从open_node回调的函数,该函数通过节点打开节点和当它击中吨他最后一项,应该是我们要选择它将使用select_node实际节点ID而不是
function open_nodes_step_by_step(remaining_nodes)
{
var nodes = remaining_nodes.slice();
tree = jQuery.jstree._focused();
if(nodes.length > 1){
var nodes_left = remaining_nodes.slice();
nodes_left.pop();
var to_open = nodes.length - 1;
tree.open_node(document.getElementById('node_' + nodes[to_open]), /*CALLBACK*/ function() {
open_nodes_step_by_step(nodes_left);
});
}
else{
tree.select_node('#node_' + nodes[0]);
}
}
我已经测试了我的IE8,FF和铬一切似乎工作得很好,在我之上的解决方案使用jQuery v1.10.1和jsTree 1.0-rc1(不幸的是,代码已经存在多年了,它拥有所有数据库和其他集成,我决定不更改为新版本,它的工作原理)
希望我帮助过有人
汤姆