View Full Version : Bug in TreeView.getNodeById

4 Apr 2007, 5:47 AM

I think it is a bug with TreeView.getNodeById
Please run the html below. getNodeById returns 'undefined'.

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Tree Test</title>

<link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" />
<script type="text/javascript" src="../yui-utilities.js"></script>
<script type="text/javascript" src="../ext-yui-adapter.js"></script>
<script type="text/javascript" src="../ext-all.js"></script>

<div id="tree-div" style="overflow:auto; height:300px;width:300px;border:1px solid #c3daf9;"></div>
<script type="text/javascript">
// yui-ext tree
var deptTree = new Ext.tree.TreePanel( 'tree-div', {
containerScroll: true

// add a tree sorter in folder mode
new Ext.tree.TreeSorter( deptTree, {folderSort:true});

// set the root node
var deptRoot = new Ext.tree.TreeNode({
text: 'Departments',
draggable:false, // disable root node dragging

var deptObj = { text: "Test", id:"1" } ;
var deptNode = new Ext.tree.TreeNode( deptObj);
deptRoot.appendChild( deptNode);

deptTree.setRootNode( deptRoot);
// render the tree

alert( 'Dept Node = ' +deptTree.getNodeById( '1'));
deptRoot.expand( false, /*no anim*/ false);


Project Management Made Easy

4 Apr 2007, 6:33 AM
I'm not sure if they changed it in beta1, but for alpha3, I was forced to recurse through my structure, and manually registerNode(), because it seems that the constructor( given a tree of json objects ) or the appendChild() doesn't do it by default

I couldn't tell if this was a bug or not.


4 Apr 2007, 7:51 AM
Nodes are registered with the Tree when they are appended/inserted (not when they are created). This allows for moving nodes from tree to tree easily.

If the parent they are being appended/inserted to is not part of a tree, there is no where to register.

If you move

deptTree.setRootNode( deptRoot);

to before you start adding nodes, they will register as expected.

4 Apr 2007, 11:02 AM
Jack, Thank you.
It worked great after I made this change.

Project Management Made Easy.

25 May 2007, 9:00 AM
I have the same issue, but Jack's answer doesn't help in this case (or I'm missing it):

var Tree = Ext.tree;

var tree = new Tree.TreePanel (
'tree-div', {
animate: true,
loader: new Tree.TreeLoader ( { dataUrl: '/account/subaccounts' } ),
enableDD: false,
containerScroll: true
} );

// set the root node
var root = new Tree.AsyncTreeNode ( {
text: 'root',
animate: true,
draggable: false,
id: '1',
cls: 'acctnode'
} );
tree.setRootNode ( root );

If I now try to use tree.getNodeById() it always returns Undefined (but I can confirm the id's are actually set as I use them later in another situation).

25 May 2007, 6:37 PM
Eh, I see the issue now. root is loaded async so it doesn't necessarily exist at the time I try to use getNodeById(). The solution would be to request it after the tree is loaded in an event handler.

However, my problem is this: I'm dealing with a framework (TurboGears) that automatically logs the user out after a specific interval. This usually involves some redirects that eventually return the user to the page they were last visiting before their session expired. On this page I have my tree control and what I'd like to do is restore the selected item in the tree that they were last viewing (the node id is stored in a cookie). However, because the parent of their last visited node may be not expanded, there's not a clear way to accomplish this except by expanding the entire tree upon their return prior to trying to move to that node.

If someone has a brilliant idea that'd be great, if not I guess they'll have to live with reselecting their last visited node.

25 May 2007, 7:01 PM
Okay, solved it. The answer was to store another cookie that contained the actual path to the node, not just the id. Then I could use this when the user selects a node:

var sm = tree.getSelectionModel ( );
sm.on ( 'selectionchange', function ( ) {
var n = sm.getSelectedNode ( );
if ( !n ) { return; }
path = n.getPath ( );
YAHOO.Tools.setCookie ( 'accountpath', path );
// other stuff...
} );

and then when the page is revisited:

root.on ( 'load', function ( ) {
path = YAHOO.Tools.getCookie ( 'accountpath' );
tree.expandPath ( path );
tree.selectPath ( path );
} );

Works like a charm.

As an aside, I found the Ext.CookieProvider not too good for general use as it encodes the cookie's value. Great for JS I'm sure, but makes it a pain when the server-side application needs the values as well.

20 Aug 2007, 4:15 AM
the tree is render();and the root is expand(true).
when i use the mehtod tree.getNodeById('1');,I get Undefined.How can i use it?

20 Aug 2007, 5:31 AM
the tree is render();and the root is expand(true).
when i use the mehtod tree.getNodeById('1');,I get Undefined.How can i use it?

i think you're better off posting this in Help instead.