Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery JsTree打开一个节点,然后选择一个子节点(使用json_结果)_Jquery_Asp.net Mvc 2_Jstree - Fatal编程技术网

Jquery JsTree打开一个节点,然后选择一个子节点(使用json_结果)

Jquery JsTree打开一个节点,然后选择一个子节点(使用json_结果),jquery,asp.net-mvc-2,jstree,Jquery,Asp.net Mvc 2,Jstree,我在MVC2项目中使用的JsTree有问题。我想创建一个函数来取消选择/关闭树上的所有节点。然后打开一个特定的节点,选择一个特定的子节点(我有这两个节点的Id值) 问题在于,select_节点总是在open_节点完成之前被调用,因此节点未被选中,因为树尚未加载数据,并且节点ID不存在 我首先尝试了这个函数 $('#demo3').jstree('deselect_all'); $('#demo3').jstree('close_all'); $('#demo3').jstree("open_no

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

问题在于,select_节点总是在open_节点完成之前被调用,因此节点未被选中,因为树尚未加载数据,并且节点ID不存在

我首先尝试了这个函数

$('#demo3').jstree('deselect_all');
$('#demo3').jstree('close_all');
$('#demo3').jstree("open_node", $('#ParentId'), false, true); 
$('#demo3').jstree("select_node", $('#ChildId'));
然后我尝试将代码移动到树的select_节点和move_节点绑定,但没有成功。目前,我无法使用setTimeout(),这是一个可怕的解决方案


有人知道我如何告诉树仅在打开完成后选择节点吗?

您可以尝试传递一个函数,将节点选择为回调,如:

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

这样,一旦打开的节点返回成功,就会调用
select\u节点。

我目前正在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 :-)
我的工作代码:

"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 :-)

希望这会有所帮助,很抱歉我没有使用json。我正在使用jstree和函数,通过单击jstree html外部的元素来打开节点

我们设置中的每个节点都像一个网页,因此在仪表板的主页上,我们有最近编辑的页面列表

这些链接中的每一个都有要执行的javascript

<a href="javascript: selectLeftNavNode(339);">Edit</a>
我们最近注意到的问题是,如果最近编辑的页面深入到树中,特别是在尚未加载的部分,它将失败

这就是为什么我决定向服务器发出ajax请求,以便检索所有父id

更改了下面的代码,在我的例子中,ajax将返回类似于以下xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<response>
<paths>
<path>339</path>
<path>338</path>
<path>38</path>
</paths>
</response>
我已经用IE8、FF和Chrome测试了我的解决方案,所有这些似乎都运行得很好,除此之外,我还使用了jQuery v1.10.1和jsTree 1.0-rc1(不幸的是,由于代码已经存在多年了,并且包含了所有数据库和其他集成,我决定不将其更改为较新版本,所以它可以工作)

希望我帮助过别人

汤姆

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]); 
    }
}