0

I have a dropdownlist of "Select an employee" which is optional. I need it to have "No one" at the top so that user can change back to NoOne if he' ve already chosen "John Someone".

My question is how to keep the prompt item, or add a dummy item at -1 without changing the dataprovider (I really hate adding "No one" to the original employeeList dataprovider)

P/S: same questions but still no answer:

Community
  • 1
  • 1
Phung D. An
  • 2,402
  • 1
  • 22
  • 23

5 Answers5

1

Just in case somebody was interested I had just found this:

Receipt

Kuba Wyrostek
  • 6,163
  • 1
  • 22
  • 40
  • this is great but i had to change the code there to work properly. I posted my change in the comments there. – Saariko Sep 16 '12 at 11:43
1

I would recommend extending the ArrayCollection class by creating a class called "DummyArrayCollection." You can store your dummy variable there and use this as a dataprovider without affecting the original list of employees:

public class DummyArrayCollection extends ArrayCollection
{
    private var _firstElement:String;
    private var _dummyArray:Array;

    public function DummyArrayCollection(source:Array=null)
    {
        super(source);
        _dummyArray = init(source);
        _firstElement = "";
    }

    public function init(src:Array):Array{
        var retArr:Array = new Array();
        retArr[0] = _firstElement; //add your dummy element here
        for(var i:int=0;i<src.length;i++){
            retArr[i] = src[i];
        }
        return retArr;
    }
}
Kyle
  • 4,202
  • 1
  • 33
  • 41
  • +1 for a good solution. For an added bonus, maybe you can add support to index the dummy item at -1, or other such functionality that provides useful features for "selectable collection" :) – drkstr May 05 '11 at 18:57
  • Look like it is impossible to reuse the "prompt" String which is my preferred solution. So your answer is chosen. Thanks – Phung D. An May 06 '11 at 04:44
  • I see a couple problems in the init() method. Line 2 in the method assigns _firstElement to retArr[0], even though _firstElement hasn't yet been initialized in the constructor. Furthermore, it subsequently overwrites retArr[0] in the for-loop. – David Siegal Jun 11 '12 at 18:16
1

Add a change event handler to your drop down list:

change = "{myDropDown.selectedIndex = -1}"

this way your prompt will be reused after item was selected.

Izabela
  • 11
  • 1
0

dropDownList.selectedItem=null

James
  • 1
0

Here is a working flex 3 example. I am sure it can be converted to flex 4 easy enough.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init( )" width="100%" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.collections.XMLListCollection;

            [Bindable]
            public var personsList:XMLListCollection;

            private function init( ):void{

                var xml:XML = 
                <root>
                    <persons>
                        <person><label>jim</label><value>jim</value></person>
                        <person><label>joe</label><value>joe</value></person>
                        <person><label>bob</label><value>bob</value></person>
                        <person><label>harry</label><value>harry</value></person>
                        <person><label>sally</label><value>sally</value></person>
                    </persons>
                </root>;

                this.personsList =  new XMLListCollection( xml.persons.person )

                var myFirstNodeXML:XML = <person><label>None Selected</label><value>none</value></person>;
                this.personsList.addItemAt(myFirstNodeXML,0);
                myCB.selectedIndex = 0;
            }

        ]]> 
    </mx:Script>
    <mx:ComboBox id="myCB" dataProvider="{this.personsList}"/>
</mx:Application>
The_asMan
  • 6,364
  • 4
  • 23
  • 34