SharpCoder
SharpCoder

Reputation: 19163

Selecting the first leaf node of a tree panel in ExtJs 4.1

I am using ExtJs 4.1 TreePanel control. When user clicks on a button, I want to select the first leaf node of the tree panel and then fire the select or itemClick event. What is the best approach for getting the desired result.

So far I am using this code:

 var root = Ext.getStore('MyStore').getRootNode();
 var firstChildNode = root.findChild('leaf', true, true);
 Ext.getCmp('treePnl').getSelectionModel().select(firstChildNode);

Is there any better way to select a node in ExtJs tree panel and how can I fire select or itemClick event of the tree panel?

Thank You!!!

Upvotes: 0

Views: 3506

Answers (2)

Eric de Groot
Eric de Groot

Reputation: 1

In case the 'leaf' is further down in the tree I used the following working solution:

// select first leaf
var item = tree.getRootNode();
while(item.firstChild)
    { item = item.firstChild; }

tree.getSelectionModel().select(item);

Upvotes: 0

Rovak
Rovak

Reputation: 778

var root = Ext.getStore('MyStore').getRootNode(),
    tree = Ext.getCmp('treePnl'),
    selModel  = tree.getSelectionModel();

selModel.select(root.firstChild);

tree.fireEvent('itemselect', root.firstChild);

Upvotes: 1

Related Questions