3 Replies Latest reply on Mar 10, 2010 8:10 AM by flexastic

    mx:tree not getting populated.. help plz!

    flexastic

      I am using Flex with BlazeDS. From Java remote object, I am sending an XML and trying to populate that in mx:tree component. The tree component shows no data. Below are code snippets -

       

      .mxml file -

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ilog="http://www.ilog.com/2007/ilog/flex" layout="absolute" autoLayout="false"
          initialize="init(); ">
          <mx:RemoteObject id="ro" destination="mgmtConsole"/>
         
          <mx:RemoteObject id="srv" showBusyCursor="true" result="resultHandler(event)" destination="mgmtConsole"/>

       

          <mx:Script>
              <![CDATA[
                  import com.jpmc.ibanker.*;
                  import mx.events.*;
                  import mx.rpc.events.*;
                  import mx.controls.Alert;
                 
                  [Bindable]
                  public var myXml:XML;
                 
                  public function init():void    {
                      srv.getRegionHierarchy();
                  }
                 
                  private function resultHandler(event:ResultEvent):void { 
                      if (event.result != null) {   
                             myXml = event.result as XML;
                        }
                  } 
              ]]>
          </mx:Script>  
         
          <mx:Tree id="tree" dataProvider="{myXml}" labelField="@label" top="321" left="10"
               height="184" width="457" showRoot="false" verticalScrollPolicy="on" 
          alternatingItemColors="[#FFFFFF,#EEEEEE]" 
          showScrollTips="true" />

      </mx:Application>

       

       

      .java -

       

          public String getRegionHierarchy() {
                      return "<root><node label=\"Parent 1\"><node label=\"Child 1\" /><node label=\"Child 2\"><node label=\"Grandchild 1\" /><node label=\"Grandchild 2\" /></node><node label=\"Child 3\" /><node label=\"Child 4\" /></node></root>";

         }

       

      Can someone please help?

        • 1. Re: mx:tree not getting populated.. help plz!
          bastek_sm

          try setting up event handlers for the server call:

           

          srv.getRegionHierarchy.addEventListener(ResultEvent.RESULT, resultHandler);

          srv.getRegionHierarchy.addEventListener(FaultEvent.FAULT, faultHandler);


          private function resultHandler(event:ResultEvent):void {

               if (event.result != null) {

                    myXml = event.result as XML;

               }

          }


          private function faultHandler(event:FaultEvent):void {

               //handle fault

          }

           

           

          Basically you must have a handler for each server function that returns some data. Hope this helps.

           

          -sebastian

          • 2. Re: mx:tree not getting populated.. help plz!
            flexastic Level 1

            I do have the event handler attached as part of <mx:RemoteObject>

             

            <mx:RemoteObject id="srv" showBusyCursor="true" result="resultHandler(event)" destination="mgmtConsole">
                </mx:RemoteObject>

             

            Also Alert.show(event.result); shows the XML returned from java object.

             

            Am I doing anything wrong with this line - myXml = event.result as XML; ?

             

            I am guessing above line does not assign the XML string coming from java to the myXml variable and as such the tree is not getting populated in <mx:tree>

            • 3. Re: mx:tree not getting populated.. help plz!
              flexastic Level 1

              Finally got it working!

               

              Used below code -

               

                                       if (event.result != null) {   
                                  var qName:QName = new QName("root");
                                  var xmlDocument:XMLDocument = new XMLDocument();
                                  var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
                                  var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(event.result, qName, xmlDocument);
                                  myXml = new XML(xmlDocument.toString());
                           }

               

              and this is tree -

               

              <mx:Tree id="tree1" dataProvider="{myXml.toString()}" labelField="@label" showRoot="false" 
                      x="10" y="37" height="117" width="195"/>