3 Replies Latest reply on Apr 21, 2011 11:52 PM by Flex harUI

    setting selectedItem on DropDownList

    paatfv10-WclpVe

      I've extended the DataGroup class to add in a new arrayCollection (var ac:ArrayCollection) storing objects that will be used by DropDownLists in each itemRenderer as dataProviders. All works fine if my ArrayCollection ac only contains one string variable per object (used by the DropDownList as dataProvider). In this case, the content of ac is defined as :

       

      for (...){

           ac.addItem("someString");

      }

       

      But I'm trying to add more variables per object, so my objects within my ArrayCollection no longer has only one variable:

       

      for (...){

           ac.addItem({name:"someString", selected:false});

      }

       

      in this second case, my objects contained in my arrayCollection has two attributes: name and selected.

       

      In my dataGroup, I am pulling into the dataProvider another arrrayCollection with two other variables: name and value. the name attributes in both arrrayCollections are matching, so the goal here is that for each item rendered in my dataGroup, there will be an item in the DropDownList that will be equivalent to the name of the object found in each itemRenderer (under data.name). This works fine, but when I come to define the selected Item from the DropDown as the data.name (as in I want to select the name of the itemRenderer's object in the DropDown), it won't work (will return null or undefined).

       

      In theory, this shuold work, right? Having set the labelField to "name" in my DropDownList, it picks the streams of attributes name in each object and displays them in each DropDownList (one for each DropDownList), but it won't select the proper item from the list. I've tried debugging and explicitely calling the set in actionscript aswell, but this doesn't work - neither does defining the selectedIndex on an arbitrary number.

       

      Here are code snippets:

       

      DataGroup Extender:

       

          public class DataGroupExtender extends DataGroup
          {

      private var _customPropertiesArray:ArrayCollection = new ArrayCollection();

       

      public function get customProperties():ArrayCollection{
                  return _customPropertiesArray
              }
             
              public function set customProperties(ac:ArrayCollection):void{ //convert the in-arrayCollection into desired structure
                  /*_customPropertiesArray = ac;*/
                  for (var i:int = 0; i < ac.length; i++){
                      _customPropertiesArray.addItem({name:(ac[i].name.toString() as String), selected:false});
                  }
              }

      }

       

      ItemRenderer:

       

      <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:mx="library://ns.adobe.com/flex/mx"
                      autoDrawBackground="false"
                      width="100%">

       

       

          <fx:Script>
              <![CDATA[

                  protected function DropDown_creationCompleteHandler(event:FlexEvent):void
                  {
                       DropDown.dataProvider = (this.parent as DataGroupExtender).customProperties;  //doesn't seem to pick this up in the mxml tags
                      /*DropDown.selectedItem = data.name; */          

                    }

       

              ]]>
          </fx:Script>

       

      <s:Group width="100%">       
              <s:layout>
                  <s:HorizontalLayout paddingTop="7" paddingLeft="10"/>
              </s:layout>
              <s:DropDownList id="DropDown" labelField="name" selectedIndex="{2}" dataProvider="{this.parent.customProperties}" width="200"
                              creationComplete="DropDown_creationCompleteHandler(event)"/>
              <s:HGroup paddingLeft="35">
                  <s:Label text="$" textAlign="right" width="10" paddingTop="7"/>
                  <s:TextInput id="textInput" width="150" text="{data.value}"/>
              </s:HGroup>
          </s:Group>

       

      </s:ItemRenderer>

       

      Again, using the debugger highlights that it traverses the code correctly, and all dataProviders and arrayCollections are set correctly, but it doesn't set the value to the selectedItem or selectedIndex values of the DropDownList. In fact, once I managed to set the selectedIndex value to 1, but it didn't set the value to _selectedIndex, which is what really needs to be set, i think.

       

      Any ideas?

       

      Pat

        • 1. Re: setting selectedItem on DropDownList
          Flex harUI Adobe Employee

          I am not sure I understand the question or code, but labelField does not

          affect selectedItem.  You have to set selectedItem to one of the objects in

          the dataProvider.  That's why strings work because they are simple objects.

          • 2. Re: setting selectedItem on DropDownList
            paatfv10-WclpVe Level 1

            I understand that labelFIeld does not affect selectedItem. If I understand it correctly, it defines the attribute upon which you want displayed in the drop down list from the objects in your dataProvider. I was hoping that this would also become some sort of set of string keys, and simply refering to a certain key from the drop down, you would be able to select a certain item. For example:

             

            You have the following arrayCollection:

            [0]     -name:row1

                      -value: 1

            [1]     -name:row2

                      -value: 2

            [2]     -name:row3

                      -value: 3

            [3]     -name:row4

                      -value: 5

             

            this array collection is your drop down's dataprovider. Now i'd specify the labelField as 'name' so that my drop down list would display:

            row1

            row2

            row3

            row4

             

            Now this is where I might be wrong, but I would expect that simply specifying the selected Item value as 'row2' would select the second row. The issue I'd have with having to pass the whole object into the selectedItem is that the dataProvider my drop down is different objects than the object sent into the itemRenderer, so the only mutual attribute they both have is this 'name' attribute, which I was hoping to use to select the correct item in the drop down. But is what you are saying is that simply passing this string key into the selectedItem attribute not possible?

             

             

             

            Thanks for your help,

            Pat

            • 3. Re: setting selectedItem on DropDownList
              Flex harUI Adobe Employee

              The selectedItem attribute isn't as smart as you wish it was.  Even if it

              was, it would have to do the same search you'll have to do on the

              dataprovider to find the index.  And sometimes, folks might already have

              that information.

               

              So, take the name and find the index and set selectedIndex.