Tree is better when you use XMLListCollection or datadescriptor to
manipulate the data

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Mayank
Sent: Wednesday, September 19, 2007 2:30 PM
To: [email protected]
Subject: [flexcoders] Strange behaviour while adding node to tree

 

Hi,

I am facing a strange behaviour in mx:Tree's rendering when i update its
data provider. 

To reproduce the problem, compile the code below and then do the
following : 
- expand the 'root' node and 'c1' node. 
- Now click on the 'add node' button. 

this is supposed to add a node under node 'c1', but the display goes
berserk when the dataprovider gets updated. However, when one tries to
expand node 'c1' everything seems to be back to normal again ... 

would appreciate any help .. 

The sample code is given below:

XMLUtils.as

package default
{
    import mx.controls.List;
    
    public class XMLUtils 
    {
        public static function getChildById ( xml : XML, id : String ) :
XML
        {
            var descendants : XMLList = xml.descendants("*");
            
            for( var i : int = 0; i <  descendants.length(); i++ )
            {
                trace ( "id - " + descendants[i].attribute ( "id" ) );

                if ( XML ( descendants[i] ).attribute("id").toString()
== id ) 
                    return descendants[i]; 

            }
            
            return null;
        }
    }
}


test.mxml

<?xml version=" 1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> " 
    layout="vertical"
    width="100%" 
    creationComplete="onCreationComplete(event)"
>
    <mx:Script>
        <![CDATA[
            import default.XMLUtils;
            import mx.rpc.events.ResultEvent;
            import mx.collections.XMLListCollection;
            import mx.collections.ArrayCollection;
            
            private function onCreationComplete ( event : Event ) : void
            {
                trace ( "onCreationComplete invoked ..." ); 
            }
            
            private function addNode ( ) : void
            {
                var newNode : XML = <node/>;
                [EMAIL PROTECTED]     = "c12";
                [EMAIL PROTECTED]        = "6";

                [EMAIL PROTECTED] = "false";
                [EMAIL PROTECTED] = "true";
                [EMAIL PROTECTED] = "true"; 
                [EMAIL PROTECTED] = "false";
                
                var node : XML = XMLUtils.getChildById( folders, "2");
                
                node.appendChild ( newNode );
            }
            
        ]]>
    </mx:Script>
    
    <mx:XML id="folders" xmlns="">
        <response>
          <node id="1" deleteAllowed="true" updateAllowed="true"
readAllowed="true" createAllowed="true" label="root"> 
            <node id="2" deleteAllowed="true" updateAllowed="true"
readAllowed="true" createAllowed="true" label="c1">
              <node id="3" deleteAllowed="true" updateAllowed="true"
readAllowed="true" createAllowed="true" label="c11"> 
                <node id="4" deleteAllowed="true" updateAllowed="true"
readAllowed="true" createAllowed="true" label="c111"/>
                <node id="5" deleteAllowed="true" updateAllowed="true"
readAllowed="true" createAllowed="true" label="c112"/> 
              </node>
            </node>
          </node>
        </response>        
    </mx:XML>

    <mx:Tree id="tree" dataProvider="{folders}" labelField="@label"
showRoot="false" width="25%" height="50%"/> 
    <mx:Button label="add node" click="addNode()"/>
</mx:Application>

 

Reply via email to