Topic: how to get node by name or id

Hi,

I need to be able to toggle selective nodes with my own javascript, I can figure out how to do node.toggle(); but how do I find the value of node.  I've added an id property to each node in the same manner that the name is set, for the purpose to give each node a unique id, that I know.

My question is, how can I get a node by id or by name? 

I've seen the recursive method, but it doesn't really seem like an optimal solution, as the tree will contain tens of thousands of nodes, and I need to optimise it for speed.

I'm looking for something like:

var myID = 10; //or whatever int my node id is
var node = tree.getNodeByID(myID);
node.toggle();

Thanks in advance.

Re: how to get node by name or id

now you can only override Mif.Tree.Node::initialize function. Something like:

TreeNodesHash={};

Mif.Tree.Node.implement({
    
    initialize: function(structure, options) {
        $extend(this, structure);
        this.children=[];
        this.type=options.type||this.tree.dfltType;
        this.property=options.property;
        this.data=options.data;
        this.state=$extend($unlink(this.tree.dfltState), options.state);
        this.$calculate();
        
        this.UID=Mif.Tree.Node.UID++;
        Mif.Tree.Nodes[this.UID]=this;
        
        TreeNodesHash[this.data.id]=this;
    }
    
});

Mif.Tree.implement({

    getNodeById: function(id){
        return TreeNodesHash[id];
    }

});

//example tree json structure
var json=[
            {
                "property": {
                    "name": "root"
                },
                "data"{
                    "id": "id1"
                }
                "children": [
                    {
                        "property": {
                            "name": "node1"
                        },
                        "data":{
                            "id": "id2"
                        }
                    },
                    {
                        "property": {
                            "name": "node2"
                        },
                        "data":{
                            "id": "id3"
                        }
                        "state": {
                            "open": true
                        },
...

3

Re: how to get node by name or id

Thank you for this good piece of code.

Here is an alternative to the previous code because TreeNodesHash always errored:

Behind

Mif.Tree = new Class({

    Implements: [new Events, new Options],
        
    options:{
        types: {},
        forest: false,
        animateScroll: true,
        height: 18,
        expandTo: true
    },

Add

// Returns a node according to the given id
    findid: function(idtofind) {
     var child=this.getrecursiveid(0,tree.root.children,idtofind);
     return child;
    },
    
    // Returns null if id has not been found, otherwise the node element with the given id
    getrecursiveid: function(id,children,idtofind)
    {
        for (var i=0,l=children.length;i<l;i++)
        {
                alert(idtofind+"=="+children[i].data.id);
                if (idtofind==children[i].data.id) 
                    {
                        return children[i];
                    }
                    else
                    {
                        if (children[i].hasChildren()) {
                        child=this.getrecursiveid2(id,children[i].children,idtofind);
                        if (child!=null) return child;
                        }
                    }
        }
        return null;
    }

Bye
Datarunner

Re: how to get node by name or id

Could somebody correct this line

child=this.getrecursiveid2(id,children[i].children,idtofind);

to

child=this.getrecursiveid(id,children[i].children,idtofind);

Thanks

Re: how to get node by name or id

TreeNodesHash always errored

?


with trunk you can

node=Mif.ids[some_id]

or add method:

Mif.Tree.implement({
    getById: function(id){
         return Mif.ids[id];
    }
})

and use:

myTree.getById(id);