Nick,
I see you have found a workaround, but I will share what I am doing to
get around this anyway... it is a fairly simple change.
 
Instead of using the itemOpen event on the tree, switch to using the
itemOpening event.  Then in your itemOpeningHandler make use of the
preventDefault() method to keep the tree from automatically expanding
the node.  After you have added all your necessary child nodes, manually
expand the node using the expandItem() function.
 
Here is the modified code (I have commented where I have made changes):
 
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; 

width="200" height="300" layout="absolute"
creationComplete="createTopNodes();">

<mx:Script>

<![CDATA[

import mx.events.TreeEvent;

import mx.collections.ArrayCollection;

[Bindable]

private var treeNodes:ArrayCollection = new ArrayCollection();

private function createTopNodes():void{

var newTestObj:Object;

for (var i:int=1; i<=20; i++){

newTestObj = new Object();

newTestObj.id = i.toString();

newTestObj.children = new Array();

treeNodes.addItem(newTestObj);

}

}

private function itemOpenHandler(e:TreeEvent):void{

//MAKE SURE THE NODE IS EXPANDING (itemOpening also gets fired if the
node is closing)

if(e.opening){

//USE preventDefault() TO KEEP THE TREE FROM AUTOMATICALLY EXPANDING THE
NODE

e.preventDefault();

var selectedObj:Object = Object(e.item);

if(selectedObj.hasChildren){

return;

}

var newTestObj:Object;

for(var i:int=1; i<=20; i++){

newTestObj = new Object();

newTestObj.id = selectedObj.id + "_" + i.toString();

newTestObj.children = new Array();

selectedObj.children.push(newTestObj);

}

selectedObj.hasChildren = true;

//MANUALLY EXPAND THE NODE

testTree.expandItem(e.item, true, true, true);

}

}

]]>

</mx:Script>

<!-- using itemOpening event instead of itemOpen -->

<mx:Tree id="testTree" width="100%" height="100%"
dataProvider="{treeNodes}" labelField="id"
itemOpening="itemOpenHandler(event);"/>

</mx:Application>

 
 

        -----Original Message-----
        From: flexcoders@yahoogroups.com
[mailto:[EMAIL PROTECTED] On Behalf Of Nick Durnell
        Sent: Thursday, April 05, 2007 4:05 AM
        To: flexcoders@yahoogroups.com
        Subject: [flexcoders] Re: Tree bug?
        
        

        Hi Robert,
        
        Here is a test app which demonstrates the problem:
        
        <?xml version="1.0" encoding="utf-8"?>
        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> " width="200"
        height="300" layout="absolute"
creationComplete="createTopNodes();">
        <mx:Script>
        <![CDATA[
        import mx.events.TreeEvent;
        import mx.collections.ArrayCollection;
        
        [Bindable] private var treeNodes:ArrayCollection = new
        ArrayCollection();
        
        private function createTopNodes():void
        {
        var newTestObj:TestObj;
        for (var i:int=1; i<=20; i++)
        {
        newTestObj = new TestObj();
        newTestObj.id = i.toString();
        newTestObj.children = new Array();
        treeNodes.addItem(newTestObj);
        }
        }
        
        private function itemOpenHandler(e:TreeEvent):void
        {
        var selectedObj:TestObj = TestObj(e.item);
        if (selectedObj.hasChildren)
        {
        return;
        }
        var newTestObj:TestObj;
        for (var i:int=1; i<=20; i++)
        {
        newTestObj = new TestObj();
        newTestObj.id = selectedObj.id + "_" + i.toString();
        newTestObj.children = new Array();
        selectedObj.children.push(newTestObj);
        }
        selectedObj.hasChildren = true;
        }
        ]]>
        </mx:Script>
        <mx:Tree id="testTree" width="100%" height="100%"
        dataProvider="{treeNodes}" labelField="id"
itemOpen="itemOpenHandler
        (event);"/>
        </mx:Application>
        
        TestObj is defined as:
        
        public class TestObj
        {
        public var id:String;
        public var hasChildren:Boolean = false;
        public var children:Array;
        }
        
        Thanks for your help.
        
        I've submitted this as a bug on Adobe's site because as Stephane

        pointed out, the tree examples on Adobe's own Quick Start pages 
        (http://www.adobe.com/devnet/flex/quickstart/working_with_tree/
<http://www.adobe.com/devnet/flex/quickstart/working_with_tree/> ) also 
        exhibit the problem. Try this on the first example application
on 
        that page:
        
        1. Expand Operations (there are no scroll bars as the tree area 
        hasn't been filled yet);
        2. Add new Operations employees until a scroll bar should
appear. 
        It doesn't.
        
        Or:
        
        1. Before expanding any nodes, add 3 Operations employees;
        2. Expand all top level nodes so that the scroll bar appears;
        3. Scroll to the top of the visible tree;
        4. Add another 3 (or more) Operations employees. Note that once
the 
        new nodes get to the bottom of the visible area, the scroll bar
does 
        not update (shrink) as it should. If you scroll down you will
see 
        that the new nodes have overwritten those in the Engineering
section.
        
        In either case if you collapse and then expand the top-level
nodes 
        the problem sorts itself out.
        
        Regards,
        
        Nick.
        
        --- In flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> , "Robert Chyko" <[EMAIL PROTECTED]>
wrote:
        >
        > I've kinda lost track of this thread, but do you have a code 
        example?
        > Something stripped down perhaps? If so I could possibly take a

        look at
        > it (I'm getting my butt kicked at work currently) but I just
went
        > through something similar here not too long ago.
        > 
        
        

         

Reply via email to