views:

1776

answers:

3

I have a simple TreePanel. I would like to select a particular node upon loading it. The nodes are from a remote file (json).

The tree is loading as expected. However, the node is not being selected. Firebug shows node as undefined. This perhaps because of the async property. But, I an unable to configure this other wise, or specify the node be selected.

Any suggestions welcomed, and thank you.

    LeftMenuTree = new Ext.tree.TreePanel({
    renderTo: 'TreeMenu',
    collapsible: false,
    height: 450,
    border: false,
    userArrows: true,
    animate: true,
    autoScroll: true,
    id: 'testtest',
    dataUrl: fileName,
    root: {
  nodeType: 'async',    
     iconCls:'home-icon',
     expanded:true,
       text: rootText
    },
    listeners: {
        "click": {
    fn: onPoseClick,
                 scope: this
               }
        },
          "afterrender": {
       fn: setNode,
       scope: this 
      }  
 });
function setNode(){
 alert (SelectedNode);
  if (SelectedNode == "Orders"){
    var treepanel = Ext.getCmp('testtest');
    var node = treepanel.getNodeById("PendingItems");
    node.select();
  }
} 
A: 

This is because the node isn't really selectable until the tree has been rendered. Try adding your node selection to an event listener listening for the render event.

Mike Clark
I tried your suggestion. I thought it will work. But, the same error, node not defined.
Natkeeran
It fires before children are rendered.
Natkeeran
Doesn't make sense to accept an answer unless it works...
AHungerArtist
A: 

I use this code in the TreeGrid to select a node

_I.treeGrid.getSelectionModel().select(_I.treeGrid.getRootNode());

I haven't tried this in a TreePanel but since the TreeGrid is based on it I'll just assume this works. I used the load event of the loader to plugin similar code after the XHR request was done, so try to write your setNode function like this:

var loader = LeftMenuTree.getLoader();
loader.on("load", setNode);    
function setNode(){
     alert (SelectedNode);
      if (SelectedNode == "Orders"){
        var treepanel = Ext.getCmp('testtest');
        treepanel.getSelectionModel().select(treepanel.getNodeById("PendingItems"));
      }
    }
SBUJOLD
A: 

I have documented a way to accomplish something very similar here:

http://www.sourcepole.ch/2010/9/28/understanding-what-s-going-on-in-extjs

what you'll need to make sure is that the node that you are selecting is visible. You can accomplish that by traversing the tree and node.expand()ing all the nodes parents (from the root down).

Tomáš Pospíšek