tags:

views:

93

answers:

1

I have a dijit.Tree that's populated by a ItemFileReadStore using JSON. Because of our setup, I need to do a new request each time a tree node is clicked. The tree is using cookies to remember which folders were expanded and not, so that's not a problem. But I'm trying to set focus to the node that was clicked.

I've managed to get the item from the store model by setting its id as a parameter in the url:

store.fetchItemByIdentity({identity:openNode, onItem:focusOpenNode(item)});
function focusOpenNode(item) {
    //I've got the item, now how do I get the node so I can do:
    var node = getNodeFromItem(item); //not a real method...
    treeControl.focusNode(node);
}

but I can't seem to find a way to get the matching node from the item id.

+1  A: 

When you create the treeControl, pass in as one of the params in the constructor params or use dojo.mixin to add to the tree widget:

/*tree helper function to get the tree node for a store item*/
getNodeFromItem: function (item) {  
    return this._itemNodesMap[item.name[0]];
}

(It would be neater to use the tree's store getAttribute to get the name of the item - but this example is not polished.)

Then you could do:

function focusOpenNode(item) {
    //I've got the item, now how do I get the node so I can do:

    var node = treeControl.getNodeFromItem(item); //now a real method...
    treeControl.focusNode(node);
}
Andy
Sweeet! I wish I could give you multiple upvotes for that one. Works like a charm. And good idea to put custom functions on the treeControl, helps clean up the code a bit.
peirix