2010-11-13 13 views
16

Sto usando il plugin JQuery jsTree, http://www.jstree.com/ sono in grado di espandere l'intero albero con il seguente metodo:jsTree aprire una filiale

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

e anche un nodo specifico:

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

Sto avendo difficoltà ad aprire un ramo dell'albero, il ramo aperto lo apre bene ma non apre il suo genitore se ne ha uno.

Qualcuno lo ha fatto con successo con jsTree? Fammi sapere se hai bisogno di maggiori informazioni.

Grazie

Eef

risposta

8

Si potrebbe usare il legame

$("#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

non dovrebbe esserci qualche tipo di 'unbind' alla fine? –

+0

@NirO. per quale ragione? –

15

il codice per ramo aperto è corretta.

Ad esempio. Fonte dell'albero:

<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> 

Aperto nodo:

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

Ha funzionato come un fascino! : D – Damiii

+0

Funziona perché hai definito i nodi in HTML, quindi sono tutti caricati e DOM viene creato nel momento in cui chiami open_node. Questo sfortunatamente non è il caso se usi i dati JSON per costruire l'albero. –

3

ho trovato il codice di Ted di lavoro, ma ho dovuto cambiare un po ':

$('#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

chi è Ted? –

+4

Ero Ted indietro nel giorno :) – Bob

8

Prova questo codice per aprire il nodo fino all'ennesima 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

Questo è in ritardo di mesi, ma questo è quello che stavo cercando, grazie. Tuttavia, manca un "});" alla fine. –

+1

Una soluzione davvero bella. – himanshupareek66

+1

ha aggiunto la chiusura '});' alla risposta per risolvere il problema individuato da @dev_row – martincarlin87

4

Ecco la funzione che può aprire un nodo specifico e tutti i suoi genitori.

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

Benvenuti in StackOverflow. Aggiungi ulteriori dettagli alla tua risposta e spiega perché questo è il modo "corretto" per farlo – Zoe

0

Nessuno dei precedenti ha funzionato per me, così ho creato questo codice, e funziona come un fascino :)

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

basta usare questo se si utilizza JSON

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