2 Replies Latest reply on Jun 13, 2010 4:18 AM by SOMEONEINTHEWORLD

    removing element

    SOMEONEINTHEWORLD

      Hello,

       

      Below is the code.  It creates new lable based on the selected item in the combobox.  When i select 1 i get example1, when i select 2 the text example 3 is coming on top of example1.  How to remove lable example1 when i select 2?

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

       

          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <fx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  import mx.controls.Text;
                 
                  import spark.components.Label;

       

                  public function combobox1_clickHandler():void
                  {
                      //var tmplbl:Label = new Label;
                      var str:String = combo1.selectedItem;
                      var arr1:Array = new Array("example1");
                      var arr2:Array = new Array("example3", "example4");
                      var y:Number = 0;
                     
                      if(str == "1")
                      {
                          for(var i:Number=0; i<arr1.length; i++)
                          {
                              var tmpLbl:Label = new Label;
                             
                              tmpLbl.text = arr1[i];
                              tmpLbl.y = y;
                              y = y + 60;
                              panel1.addElement(tmpLbl);   
                          }                   
                      }
                     
                      if(str == "2")
                      {
                          for(var i:Number=0; i<arr2.length; i++)
                          {
                              var tmpLbl:Label = new Label();
                             
                              tmpLbl.text = arr2[i];
                              tmpLbl.y = y;
                              y = y + 60;                               
                              panel1.addElement(tmpLbl);                   
                          }               
                      }

       

                  }

       

                 
                 
              ]]>
          </fx:Script>
          <s:Panel id="panel1" x="192" y="245">
             
          </s:Panel>
          <s:ComboBox id="combo1" width="140" close="combobox1_clickHandler()" x="192" y="170">
              <s:dataProvider>
                  <mx:ArrayList>
                      <fx:String>1</fx:String>
                      <fx:String>2</fx:String>
                      <fx:String>3</fx:String>
                      <fx:String>4</fx:String>
                  </mx:ArrayList>
              </s:dataProvider>
          </s:ComboBox>
      </s:Application>

        • 1. Re: removing element
          David_F57 Level 5

          hi,

           

          I adjusted you code a bit to make it more dynamic....the idea is that you may have data attached to each item in the combobox so eventually you could use the code no matter how many items with subdata there is. Things like arraycollections could be populated earlier in the app etc...

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

             xmlns:s="library://ns.adobe.com/flex/spark"

             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

           

          <fx:Declarations>

          <!-- Place non-visual elements (e.g., services, value objects) here -->

          </fx:Declarations>

           

          <fx:Script>

          <![CDATA[

          import mx.collections.ArrayCollection;

          import mx.controls.Alert;

          import mx.controls.Text;

           

          import spark.components.Label;

           

          public function combobox1_clickHandler():void

          {

          var arrCollection:ArrayCollection=new ArrayCollection();

          arrCollection.addItem( new Array("example1"));

          arrCollection.addItem( new Array("example3", "example4"));

          if (combo1.selectedIndex < arrCollection.length)

          {

          panel1.removeAllElements();

          for(var i:Number=0; i < arrCollection[combo1.selectedIndex].length; i++)

          {

          var tmpLbl:Label = new Label;

          tmpLbl.text = arrCollection[combo1.selectedIndex][i];

          panel1.addElement(tmpLbl);   

          }

          }

          }

          }

          ]]>

          </fx:Script>

          <s:Panel id="panel1" x="192" y="245">

          <s:layout>

          <s:VerticalLayout/>

          </s:layout>

          </s:Panel>

          <s:ComboBox id="combo1" width="140" close="combobox1_clickHandler()" x="192" y="170">

          <s:dataProvider>

          <mx:ArrayList>

          <fx:String>1</fx:String>

          <fx:String>2</fx:String>

          <fx:String>3</fx:String>

          <fx:String>4</fx:String>

          </mx:ArrayList>

          </s:dataProvider>

          </s:ComboBox>

          </s:Application>

          • 2. Re: removing element
            SOMEONEINTHEWORLD Level 1

            Thanks.

             

            Thats what i was looking for.