2010-11-13 37 views
16

estoy usando el plugin de jQuery jsTree, http://www.jstree.com/ soy capaz de expandir todo el árbol con el siguiente método:jsTree abrir una sucursal

$("#tree").jstree("open_all"); 

y también un nodo específico:

$("#tree").jstree("open_node", $('#childNode')); 

Tengo dificultades para abrir una rama del árbol, la rama abierta la abre bien pero no abre la carpeta principal si tiene una.

¿Alguien ha hecho esto con éxito con jsTree? Hágame saber si necesita más información.

Gracias

Eef

Respuesta

8

Usted podría utilizar la unión

$("#tree").bind("open_node.jstree", function (event, data) { 
    if((data.inst._get_parent(data.rslt.obj)).length) { 
    data.inst._get_parent(data.rslt.obj).open_node(this, false); 
    } 
}); 
+2

¿no debería haber algún tipo de 'desvinculación' al final? –

+0

@NirO. ¿por qué razón? –

15

Su código de sucursal abierta es correcta.

Por ejemplo. Fuente del tronco:

<div id="treeTask"> 
     <ul> 
      <li id="node_37"><a href="#">TEST1</a> 
       <ul> 
        <li id="node_38"><a href="#">TEST2</a></li> 
        <li id="node_39"><a href="#">TEST3</a></li> 
       </ul> 
      </li> 
     </ul> 
    </div> 

nodo abierto:

$("#treeTask").jstree("open_node", $("#node_38")); 
+0

¡Funcionó como un encanto! : D – Damiii

+0

Esto funciona porque ha definido nodos en HTML, por lo que todos se cargan y DOM se construye en el momento en que llama a open_node. Desafortunadamente, este no es el caso si usa datos JSON para construir el árbol. –

3

i encontrado código de Ted trabajo, pero tuvo que cambiar un poco:

$('#jsTree').bind("open_node.jstree", function (event, data) { 
     if((data.inst._get_parent(data.rslt.obj)).length) { 
     data.inst.open_node(data.inst._get_parent(data.rslt.obj), false,true); 
     } 
    }); 
+6

¿quién es Ted? –

+4

Yo era Ted en el día :) – Bob

8

Pruebe este código para abrir el nodo hasta el n ° Le. vel

$("#myTree").jstree({options}).bind('loaded.jstree', function (e, data) { 
    /** 
    * Open nodes on load (until x'th level) 
    */ 
    var depth = 3; 
    data.inst.get_container().find('li').each(function (i) { 
     if (data.inst.get_path($(this)).length <= depth) { 
      data.inst.open_node($(this)); 
     } 
    }); 
}); 
+1

Esto es meses tarde, pero esto es lo que estaba buscando, gracias. Sin embargo, falta un '});' al final. –

+1

Una solución realmente agradable. – himanshupareek66

+1

agregó el cierre '});' a la respuesta para resolver el problema detectado por @dev_row – martincarlin87

4

Aquí está la función que puede abrir un nodo específico y todos sus padres.

function expandNode(nodeID) { 
    // Expand all nodes up to the root (the id of the root returns as '#') 
    while (nodeID != '#') { 
     // Open this node 
     $("#jstree").jstree("open_node", nodeID) 
     // Get the jstree object for this node 
     var thisNode = $("#jstree").jstree("get_node", nodeID); 
     // Get the id of the parent of this node 
     nodeID = $("#jstree").jstree("get_parent", thisNode); 
    } 
} 
0
// Expand pasted, dragged and dropped node for jstree 3.3.1 
     var objtree = $('#jstree'); 
     objtree.bind('paste.jstree', function(e, d) { objtree.jstree('open_all', '#' + d.parent); }); 
     $(document).bind('dnd_move.vakata', function(e, d) { objtree.jstree('open_all', $(d.event.target).closest('.jstree-node').attr('id')); }); 
     $(document).bind('dnd_stop.vakata', function(e, d) { objtree.jstree('open_all', $(d.event.target).closest('.jstree-node').attr('id')); }); 
+1

Bienvenido a StackOverflow. Agregue más detalles a su respuesta y explique por qué esa es la forma "correcta" de hacerlo – Zoe

0

Ninguno de los anteriores trabajó para mí, así que he creado este código, y funciona como un encanto :)

$('#tree').on('open_node.jstree', function (event, data) { 
    if(data.node.parent !== "#") { 
     data.instance.open_node(data.node.parent); 
    } 
}); 
0

sólo tiene que utilizar esta opción si utiliza JSON

$("#treeId").on 
('loaded.jstree', function() { 
$("#treeId").jstree("open_node", $("#nodeId")); 
}); 
Cuestiones relacionadas