Node Open at a specific level of the treegrid

Following your answer on Mark Wolfgramme question I have tried your solution but I am not sure exactly where I should put that part of code. I am trying to open all the node of the tree at the same time when clicking on the 2nd level of the tree.

The 2 first nodes have a collapsed status and when clicking on the 2nd node I would like the tree to expand all the nodes (ex. : 7 levels) at the same time.



I have tried with open tag in my XML file but it is not doing what I am expecting.



I have created a function showNodesToLevel and places it in the dhtmlXTreeGrid.js. Here is an example of my code :



dhtmlXGridObject.prototype.showNodesToLevel=function()

{

this._h2.forEachChild(0,function(el)

{

if (el.level == 2)

this.openItem(el.id);

});

};



I am calling this code on my client-side with



mygrid = new dhtmlXGridObject(‘gridbox’);

.

.

.

mygrid.showNodesToLevel();

mygrid.init();

mygrid.loadXML(“GetDataXML.ashx?Grid=2”);



but this is not working at all…can you tell me where you would put or use your the part of code. Thanks, Jayeff



Here is Mark post :



Question posted by Mark Wolfgramme on Nov 18, 2008 05:58

    open in interactive version

Tree/grid exposed level



Is there a function which expands/collapses a tree to a specified level? E.g. myTreeGrid.showNodesToLevel(2) which would only show the tree nodes up to the 3rd level (automically collapsing/expanding as necessary).



Answer posted by Support on Nov 18, 2008 07:18

Nope, unfortunately there is no such function, but next code can be used for the same task



treegrid._h2.forEachChild(0,function(el){

if (el.level == 1)

treegrid.openItem(el.id);

});

but this is not working at all…
The code need to be called after data loading, when information about elements available, to do so, change the code as


mygrid.init();
mygrid.loadXML(“GetDataXML.ashx?Grid=2”,function(){
//code here will be called only after data loading, onXLE event can be used for the same purpose
mygrid.showNodesToLevel();
});

With such code, after data loading tree will expand two upper levels

>>I would like the tree to expand all the nodes (ex. : 7 levels) at the same time.
If you want to expand all sub-hierarchy when parent item opened - it can be done as

mygrid.atachEvent(“onOpenEnd”,function(id,state){
if (state==1) //parent element opened
mygrid._h2.forEachChild(id,function(el){ //for each child node of opened one
mygrid.openItem(el.id);
})
})


it probably not very fast solution, because of mass rendering calls, but more optimal solution can be created only through direct manipulation with treegrid structure.



Can you tell me in which js file the onOpenend event is located? I am looking at the dhtmlxtreegrid.js and the only think that I can see is  this function :

dhtmlXTreeObject.prototype.setOnOpenEndHandler=function(func)
 {
     this.attachEvent(“onOpenEnd”,func);
 };

but there is no trace of the “onOpenEnd” function.

Thanks a lot. Jayeff

Can you tell me in which js file the onOpenend event is located?
dhtmlxtreegrid.js, line 148
dhtmlXGridObject.prototype.collapseKids=function(curRow){

this.callEvent(“onOpenEnd”,[curRow.idd,-1]);

Which version of treegrid you are using?

Fantastic, It was there. I was not checking right thing.
Here is the code :

        mygrid = new dhtmlXGridObject(‘gridbox’);
       .
       .
       .
        mygrid.init();
        mygrid.loadXML(“GetDataXML.ashx?Grid=2”);
        mygrid.attachEvent(“onOpenEnd”,function(id,state){ var levl =mygrid.getLevel(id); if (state==1 && (levl==“1”)) mygrid._h2.forEachChild(id,function(el){ mygrid.openItem(el.id);})});

I am using Professional version.
Thanks again, Jayeff