10 Replies Latest reply on Aug 25, 2010 2:56 PM by advernut

    Setting the selectedIndex of a DropDownList




      I quess there is a really simple solution to this but I just can't figure it out...so here it goes:


      I'm showing a list of contact types in a DropDownList ("person", "radiochannel" etc). These contact types are bound to an arraycollection which gets it's values from database via httprequest.

      In the dropdowlist I am showing the name of a contact type.


      In Datagrid I am showing a list of contacts which all have a contact type saved in db (contact.contactType_id = contactType.contactType_id)

      When user selects a row in the datagrid, I am showing the contact's data in a form which also has this contact type-DropDownList. I need to show the contact's contact type selected in this DropDownList. The problem is now that when I select the contact's contactType_id from the datagrid and do this:


      cmbContactTypes.selectedIndex = dgContacts.selectedItem.contactType_id;


      it actually selects the index-value with that id-number, not the contactTypes actual ID. For example in database the "Website" ID is 1 but the contact type DropDownList may have types like this:


      Person (index 0)

      Radiochannel (index 1)

      Website (index 2)


      How could I select the row with the actual database-ID from the contactType DropDownList instead of the index-value in that list?


      Thanks in advance.

        • 1. Re: Setting the selectedIndex of a DropDownList
          Flex harUI Adobe Employee

          I think you have to search the DDL's dataProvider until you find the matching id then set that index


          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

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

          • 2. Re: Setting the selectedIndex of a DropDownList
            liquid8x Level 1

            Thank you. I hadn't thought about that, i completely forgot that I could use the dataprovider to fetch the correct id.

            I got it working now.

            • 3. Re: Setting the selectedIndex of a DropDownList

              Hey liguid8x,


              I'm having a very similar problem, but I didn't quite catch the solution.  I think it would help if I could see some code.


              My problem is this.  I have a standard CRUD app except for the dropdownlist I added.  Everything works except for my Update function.  I assumed that I would need to add a field to my database table that populates my datagrid, which is what I did.  I first generated the forms (detail, Add and Update) from my datagrid and then replaced the matching textfields with my dropdownlist. The dropdownlist is populated with remote data (I want to be able to update the list dynamically).  I have been able to get everything working correctly, except for the Update. I think the problem is that the datagrid is not picking up the selectedIndex from the dropdownlist. I still new to AS and not sure how the set up the event handler correctly.


              I have searched everywhere (Adobe, internet, etc.) and your post is the closest thing that matches my problem I could find so far.


              If you can help me, it would be greatly appreciated!



              • 4. Re: Setting the selectedIndex of a DropDownList
                liquid8x Level 1



                This is what I did, this is a part of a function which is called by datagrid's change-action when a row is selected from it:


                        for (var i:int=0; i<kontaktityypitData.length; i++)
                                    if ( kontaktityypitData.getItemAt(i).id.toString() == dgKontaktit.selectedItem.kontaktityyppi.toString() )
                                        cmbKontaktityypit.selectedIndex = i;


                Short explanation:

                dgKontaktit = Datagrid holding all the contacts (dgKontaktit.selectedItem.kontaktityyppi.toString() returns the contact-type's ID)

                kontaktityypitData = ArrayCollection holding the contact-types

                CmbKontaktityypit = Combobox for the contact types.


                So the code loops through the arrayCollection which is set as dataProvider for the combobox. When the datagrid's selecteditem's id is found then the combobox's selectedIndex is set to the same id.

                • 5. Re: Setting the selectedIndex of a DropDownList
                  advernut Level 1

                  It worked!  You are awesome!!!


                  Just one odd behavior though...  when I first launch the app and click on a row in the datagrid and then select the Update button, the Dropdownlist is blank. ??  But then when I select another row and then the Update button it works as expected.  Any idea what's going on here?


                  Here's the function:


                  protected function NTdataGrid_changeHandler(event:ListEvent):void



                       for (var i:int=0; i<getAllInventoryStatusResult.lastResult.length; i++)


                                                if (getAllInventoryStatusResult.lastResult.getItemAt(i).invent_id.toString() == NTdataGrid.selectedItem.status_id.toString() )


                                                          StatusDDL.selectedIndex = i;






                  Thanks for you help in advance.

                  • 6. Re: Setting the selectedIndex of a DropDownList
                    advernut Level 1

                    I think I fixed it.  I needed to make the Update dropdownlist controller's selection required.

                    • 7. Re: Setting the selectedIndex of a DropDownList
                      advernut Level 1

                      Nope.  It's still not working right on the first click on the update button after launch.  At least the dropdownlist is not blank now, but it chooses the first item in the list (0).  Works after that.  Any ideas?

                      • 8. Re: Setting the selectedIndex of a DropDownList
                        advernut Level 1

                        I quit the browser to make sure it was not caching and now the app"s throwing an error when I click the datagrid row:  TypeError: Error #1010: A term is undefined and has no properties.


                        Not sure where this came from.  It was working pretty well before. ??

                        • 9. Re: Setting the selectedIndex of a DropDownList
                          liquid8x Level 1

                          Hmmm, bit difficult to say... maybe there is something fetched differently after the update compared to the first time the data is fetched?

                          Have you checked from db that is the id value saved correctly after the first update?

                          I'd suggest to log/debug your application and check out what are the ID-values that are moved during updates and during the launch.

                          • 10. Re: Setting the selectedIndex of a DropDownList
                            advernut Level 1

                            It looks like from debugging, I didn't defined "i" correctly, but not sure what I need to do to fix it.