5 Replies Latest reply on Jan 18, 2010 10:01 PM by harsh_hot

    mxml code in script

    harsh_hot

      Hi All,

       

      I want to write mxml code (some tags and all) inside <mx:Script> based on some conditions. For example, if the value>10 , i want to display a textbox and if value <10 I want to display image.

       

      How do we dynamically write mxml code inside script.

       

      In Java, we use out.println() and write whatever we want but how to do this in Flex.

       

      Pls suggest

        • 1. Re: mxml code in script
          RK... Level 3
          1 person found this helpful
          • 2. Re: mxml code in script
            Flex harUI Adobe Employee

            <mx:TextInput visible="{value > 10}" />

             

            Use trace() instead of out.println()

            • 3. Re: mxml code in script
              msakrejda Level 4

              You may also want to use component states (see Flex docs) and conditionally add or remove child components.

              • 4. Re: mxml code in script
                archemedia Level 4

                If you want to generate display objects at runtime, you should have a parent container to which you add and remove child objects through actionScript.

                 

                 

                SCRIPT:

                 

                private function generate(whatever:String):void

                {

                     container.removeAllChildren();

                 

                     switch (whatever)

                     {

                          case "Button":

                             var b:Button = new Button();

                               container.addChild(b as displayObject);

                               break;

                          case "vbox":

                             var v:VBox = new VBox();

                               container.addChild(v as displayObject);

                               break;

                 

                 

                     }

                }

                 

                 

                MMXL:

                <VBox id="container"/>

                • 5. Re: mxml code in script
                  harsh_hot Level 1

                  Thanks everyone for your helpful responses. Now i am facing a problem while adding components. My motive is have a grid on a canvas and i again want to add grid as griditem of the previous grid, i.e, one main grid and all the griditems of this main grid will again contain one grid. For this purpose, i am declaring main grid in class and initializing it inside init(). Now i am calling a function from init() which in turn is going to put grids as part of griditem of the main grid. Please refer to the code below.

                   

                   

                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="httpid.send();init();" width="1000" height="800">
                  <mx:Canvas>
                  <mx:Script>
                      <![CDATA[
                          import mx.controls.Image;
                          import mx.containers.GridItem;
                          import mx.containers.GridRow;
                          import mx.containers.Grid;
                     
                          var maingrid:Grid=null;
                          var mainrow1:GridRow=null;
                          var mainitem1:GridItem=null;
                          var mainrow2:GridRow=null;
                          var mainitem2:GridItem=null;
                          var mainitem3:GridItem=null;
                          var mainitem4:GridItem=null;

                   

                            private function init():void{

                   

                             maingrid=new Grid();
                             mainrow1=new GridRow();
                             mainitem1=new GridItem();
                             mainrow2=new GridRow();
                             mainitem2=new GridItem(); 
                             mainitem3=new GridItem(); 
                             mainitem4=new GridItem();

                   

                            xmlLoaded();

                            }

                   

                  function xmlLoaded():void{

                   

                  var grid:Grid=new Grid();
                      var row:GridRow=new GridRow();
                      var item1:GridItem=new GridItem();
                      var item2:GridItem=new GridItem();
                      var item3:GridItem=new GridItem();
                      var txt1:Text=new Text();
                      var img:Image=new Image();     
                     
                      txt1.text='abc';
                      img.source='assets/12.jpg';
                      item1.addChild(txt1);
                      item2.addChild(img);
                      row.addChild(item1);
                      row.addChild(item2);
                      row.addChild(item3);
                      grid.addChild(row);   
                      mainitem1.addChild(grid);
                      maingrid.addChild(mainrow2);
                      mainrow1.addChild(mainitem1);
                      mainrow1.addChild(mainitem2);
                      mainrow1.addChild(mainitem3);
                      mainrow1.addChild(mainitem4);
                      maingrid.addChild(mainrow1);

                   

                       canvas.addChild(maingrid);

                  }

                   

                  }
                    
                  }       
                      ]]>
                     
                      </mx:Script>

                   

                  <mx:Canvas id="canvas" />
                     
                  </mx:Application>

                   

                   

                  The problem is , right now it doesnt display anything but when i declare and initilize maingrid and its rows/items inside xmlLoaded() then it works but it foils my purpose to have only one main grid. Please shed some light on this as to why this is happening.

                   

                  Also, can u please suggest how to catch exceptions in flex. I am clueless as everytime a  blank page appears and no error or warning comes.