getting parent nodes

2,673 views
Skip to first unread message

John Asmuth

unread,
Mar 24, 2011, 1:35:03 PM3/24/11
to jst...@googlegroups.com
Hi, more newb questions.

I'm trying to open all the ancestors of a node in the tree. This is so when I select it, you can actually see it. This is my attempt at code to do this:


var OpenRecurs = function($t, $c) {

var $p = $t.jstree("_get_parent", $c)

if ($p.attr("id") != $c.attr("id")) {

OpenRecurs($t, $p)

}

$t.jstree("open_node", $c)

}

var SelectInTree = function($t) {

var $n = $(sel)

OpenRecurs($t, $n)

$t.jstree("deselect_all")

$t.jstree("select_node", $n)

}

SelectInTree($("#pkgbrowser"))


Here sel is the selector used for the child node. It is selecting the node successfully, but it is not opening the parents. $p ends up being the div containing the jstree as a whole.


Any pointers?


Also, is there a way I could do something like $t.select_node($n) instead of $t.jstree("select_node", $n)?


- John

John Asmuth

unread,
Mar 24, 2011, 1:47:09 PM3/24/11
to jst...@googlegroups.com
I found a solution by searching the group: https://groups.google.com/d/topic/jstree/p0BeAjiGYIA/discussion

Though I don't really understand it, or understand why what I tried doesn't work.

mvwd

unread,
Mar 25, 2011, 3:57:35 AM3/25/11
to jsTree
Selecting a node will automatically open it's ancestors.

Possible ways to interact with the tree (take a look at the core
docu!!):

$.jstree._focused().select_node('#nodeID');
$.jstree._reference('#treeID').select_node('#nodeID');
$('#treeID').jstree('select_node', '#nodeID');

Markus.

BTW: your link (to google groups) is not working for me.

John Asmuth

unread,
Mar 25, 2011, 7:41:07 PM3/25/11
to jst...@googlegroups.com, mvwd


On Friday, March 25, 2011 3:57:35 AM UTC-4, mvwd wrote:
Selecting a node will automatically open it's ancestors.

This doesn't seem to be the case. The node would be selected with its parent closed. When I expanded the parent, I'd see the node already selected.

Adding the following code fixed the issue.

var SelectAux = function (e, data) {
  var SelectOpen = function () {
    if (!data.inst.is_open(this)) {
      data.inst.open_node(this);
    }
  }
  data.rslt.obj.parents('.jstree-closed').each(SelectOpen)
}
$("#filebrowser").bind("select_node.jstree", SelectAux) 

$.jstree._reference('#treeID').select_node('#nodeID'); 

This may be what I was looking for. Thanks! 

Toby

unread,
Apr 3, 2011, 11:12:38 AM4/3/11
to jsTree
John, I'm using 1.0rc2 and also found that selecting a node does not
open the parents automatically, might be doing something wrong,
but.... Anyway, I found this code somewhere around here and bind it
in...

// select/onclick for tree
.bind("select_node.jstree", function (e, data) {
// open path for parents
data.rslt.obj.parents('.jstree-closed').each(function() {
data.inst.open_node(this);
});
})

Note that my trees are single select (UI "select_limit":1) and when
progamatically triggering a select, it looks like this:

$("#myTree").jstree("deselect_all");
$("#myTree").jstree("select_node", "#"+key);
Reply all
Reply to author
Forward
0 new messages