4 Replies Latest reply on Mar 9, 2011 1:14 AM by Flex harUI

    ComboBox selectedIndex returns -1 in Flex 4.0 and blank in 3.5

    madhavi.agnihotri

      I have a combo box in my application which is populated by a database select query.In some scenarios, the combo box should have no values.

      When I use Flex 4.0, I check if combobox.selectedIndex <= 0 , then do some processing else get the selectedItem.

      So , if the combo box has no values, it returns selectedIndex = -1.

      Now the issue is that in production my application uses Flex 3.5.The selectedIndex in such case in Flex 3.5 returns as blank.

      I tried checking the following:

       

      if(isNaN(view.cbo.selectedIndex) ){

        Alert.show("in if ");

      }else if(view.cbo.selectedIndex.toString() == "undefined") {

      Alert.show("in if undefined "+view.cbo.selectedIndex.toString());

      }else if (view.cbo.selectedIndex == undefined){

      Alert.show("undefined "+view.cbo.selectedIndex);

      }else {

      Alert.show("in else "+view.cbo.selectedIndex);

      }

      But the selectedIndex returns nothing.Whereas in 4.0 it prints it as -1.

       

      Please suggest a solution.

       

       

       

        • 1. Re: ComboBox selectedIndex returns -1 in Flex 4.0 and blank in 3.5
          Nuwan,Chamara

          I think you can use the selectedItem in the combo box, instead of using selectedIndex property.

          • 2. Re: ComboBox selectedIndex returns -1 in Flex 4.0 and blank in 3.5
            snkd Level 1

            hi,

            in your logic combobox.selectedIndex <= 0 should be

            combobox.selectedIndex < 0

            because if combobox.selectedIndex = 0 mean there are some selected items..dont forget zero based indexing in combobase class in flex

             

            thats according to my knowlege,try the following and understand

             

             

            mx.controls.ComboBox.selectedIndex(value:int):void
            The index in the data provider of the selected item. If there is a  prompt property, the selectedIndex value can be set to  -1 to show the prompt. If there is no prompt, property then  selectedIndex will be set to 0 once a dataProvider is  set.

            If the ComboBox control is editable, the selectedIndex property  is -1 if the user types any text into the text field.

            Unlike many other Flex properties that are invalidating (setting them does  not have an immediate effect), the selectedIndex and  selectedItem properties are synchronous; setting one immediately  affects the other.

            This property can be used as the source for data binding.

             

            spark.components.ComboBox.selectedIndex(value:int):void
            The 0-based index of the selected item, or -1 if no item is selected.  Setting the selectedIndex property deselects the currently selected  item and selects the data item at the specified index.

            The value is always between -1 and (dataProvider.length - 1). If  items at a lower index than selectedIndex are removed from the  component, the selected index is adjusted downward accordingly.

            If the selected item is removed, the selected index is set to:

            • -1 if requireSelection = false or there are no  remaining items.
            • 0 if requireSelection = true and there is at least  one item.

            When the user changes the selectedIndex property by interacting  with the control, the control dispatches the change and  changing events. When you change the value of the  selectedIndex property programmatically, it dispatches the  valueCommit event.

            This property can be used as the source for data binding.

            • 3. Re: ComboBox selectedIndex returns -1 in Flex 4.0 and blank in 3.5
              madhavi.agnihotri Level 1

              I used combobox.selectedIndex < 0  check, but even that doesn't work

              The issue is that when I do combobox.selectedIndex < 0 in Flex 4.0, it works fine and the value of selectedIndex = -1 ,but in Flex 3.5, if I put an alert to print selectedIndex, it prints nothing.

              I also tried

              If( combobox.selectedItem == null),bit never enters the loop.

              Please suggest any other alternative for Flex 3.5

              • 4. Re: ComboBox selectedIndex returns -1 in Flex 4.0 and blank in 3.5
                Flex harUI Adobe Employee

                I tried using Alert in 3.5 and it showed -1.  Post a 20-line test case

                showing your problem.