getting a dojo node when I have the store item id

2019-08-01 17:59发布

问题:

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:

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);
}


回答2:

Great answer, I've seen this done in a variety of ways, but this is the simplest (and best).

I had to modify it a bit because of how I'm using the control.

Instead of this._itemNodesMap[item.name[0]], I use this._itemNodesMap[item.id]



标签: dojo