4 Replies Latest reply on May 21, 2009 4:39 PM by Flex harUI

    generic way to get children recursivly

    run,ryan! Level 3

      what's the generic way to get children recursively?

       

      eg. I have a Canvas with following structure

       

      canvasA

         canvasB

             imageA

             imageB

         canvasC

             canvasD

         ImageC

       

      now in order to get a list of all the children in canvasA, which lincudes {canvasB, canvasC, canvasD, imageA, imageB, imageC}

      I have to for loop twice:

      for each(var dispObj1:displayObject in canvasA.getChildren())
      {
        if(dispObj1 is canvas)
        {
          var childCanvas:Canvas = dispObj1 as Canvas;
          for each(var dispObj2:DisplayObject in childCanvas.getChildren())
          {
            //create list
          }
        }
      }
      

      which is not neat and generic,

      so do you have a better way? thanks

        • 2. Re: generic way to get children recursivly
          Gregory Lafrance Level 6

          This seems to work. If it answers your question or helps, please mark it as such.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();">
            <mx:Script>
              <![CDATA[
                import mx.controls.Alert;  
                import mx.core.Container;
                    
                private function init():void{
                  checkChildren(this);
                }
                
                private function checkChildren(objToCheck:Object):void{
                          for each(var obj:Object in objToCheck.getChildren()){
                    if(obj is Container){
                      mx.controls.Alert.show("Container:" + obj.toString());
                      checkChildren(obj);
                    }else{
                      mx.controls.Alert.show("Not container:" + obj.toString());
                    }
                          }
                }
              ]]>
            </mx:Script>
            <mx:VBox>
              <mx:Button id="btn1"/>
              <mx:CheckBox id="chbx"/>
              <mx:ComboBox>
                <mx:String>one</mx:String>
                <mx:String>two</mx:String>
                <mx:String>three</mx:String>
              </mx:ComboBox>
            </mx:VBox>
            <mx:Canvas>
                 <mx:Button id="btn2"/>
            </mx:Canvas>
          </mx:Application>
          
          1 person found this helpful
          • 3. Re: generic way to get children recursivly
            run,ryan! Level 3

            so I think this is the right answer... and the only answer?

            • 4. Re: generic way to get children recursivly
              Flex harUI Adobe Employee

              Are you referring to Greg's code?  It is correct in principle, but it is probably more efficient to use numChildren and getChildAt().  It also doesn't really know whether a child is intended to be a container or is intended to be a component but just happens to extend a Container, although there is no real good test for that.  IOW, if you supplied a black box chart widget but based it on Container, this algorithm will look inside the widget and report on its children, but not on the children of a non-Container like ComboBox.

               

              And the actual algorithm also depends on what kind of search you want (breadth-first, depth-first)

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui