2011-09-05 104 views
5

我遇到了我在MVC2项目中使用的JsTree问题。我想创建一个函数来取消选择/关闭树上的所有节点。然后打开一个特定的节点,并选择一个特定的子节点(我有两个Id值)。JsTree打开一个节点,然后选择一个子节点(使用json_result)

问题在于select_node总是在open_node完成之前调用,所以没有选择节点,因为树还没有加载数据,并且节点ID不存在。

我第一次尝试这个功能。

$('#demo3').jstree('deselect_all'); 
$('#demo3').jstree('close_all'); 
$('#demo3').jstree("open_node", $('#ParentId'), false, true); 
$('#demo3').jstree("select_node", $('#ChildId')); 

然后我试着将代码移动到select_node和move_node绑定树,但没有运气。目前我被卡住了setTimeout(),这是一个可怕的解决方案。

有谁知道我可以告诉树只有在打开完成后才选择节点?

回答

7

你可以尝试通过该选择节点像一个回调函数:

$('#demo3').jstree('open_node', '#ParentID', function(e, data) { 
    $('#demo3').jstree('select_node', '#ChildId'); 
}, true); 

这样一旦open_node返回成功select_node将被调用。

1

我目前在一个MVC4项目中使用它。

如果您将open_node函数配置为加载节点JSON(在“core”插件中将load_open设置为true),则新添加的节点存在于服务器端,但其DOM元素仍然不是因为open_node功能没有完成它的工作。因此,您需要等待或使用第二个参数(成功回调)。在回调中,在DOM树中呈现的新节点及其选择器有效。

jsTree配置示例:

"core": { 
    "open_parents": true, 
    "load_open": true 
} 

我的工作代码:

$("iframe#UploadTarget").load(function() { 
    var result = jQuery.parseJSON($(this).contents().text()); 
    if (result.Result == true) { 
    $("#uploadDialog").dialog("close"); 
    var tree = jQuery.jstree._focused(); 

    /* 
     open_node will open the parent, get the childs from the server 
     (controller) and renders the new item before the callback executed, 
     so the jQuery selector will be valid 
    */ 

    tree.open_node(result.ParentId,/*CALLBACK*/ function() { 
      var newNode = $("#" + result.Id); 
      tree.select_node(newNode, false, null); 
    }); 

    } else { 
     alert(result.Result + ":" + result.ResultDescription); 
    } 

});//GOOD LUCK :-) 
1

希望这能帮助和抱歉,我没有使用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(不幸的是,代码已经存在多年了,它拥有所有数据库和其他集成,我决定不更改为新版本,它的工作原理)

希望我帮助过有人

汤姆

相关问题