PDA

View Full Version : tree getNodeByProperty



cgoss
28 Jun 2007, 11:42 AM
Thought I'd share this helpful function that Yahoo supports in their tree that I've adapted for use in Ext.

/**
* Returns a node that has a matching property and value in the data
* object that was passed into its constructor.
* @method getNodeByProperty
* @param {object} property the property to search (usually a string)
* @param {object} value the value we want to find (usuall an int or string)
* @return {Node} the matching node, null if no match
*/
Ext.data.Tree.prototype.getNodeByProperty = function(property, value) {
for (var i in this.nodeHash) {
var n = this.nodeHash[i];
if (n.attributes && value == n.attributes[property]) {
return n;
}
}

return null;
};

Usage:

var node = tree.getNodeByProperty('uniqueId','unique_'+someId);

Animal
28 Jun 2007, 11:55 PM
Ext has the more flexible node method: http://extjs.com/deploy/ext-1.1-beta1/docs/output/Ext.tree.TreeNode.html#findChildBy

Usage:



var node = tree.getRootNode().findChildBy(function(n) {
if (n.attributes.uniqueId == 'unique_' + someId) {
return true;
}
};

cgoss
29 Jun 2007, 6:28 AM
Thanks Animal, that does the job. I didn't notice this because the paradigm of Ext is to hang node functions on the nodes themselves, as opposed to the tree like Yahoo does. Ext makes more sense in a lot of ways. :D