开发者

How to keep prompt/dummy item in flex4 dropdown list

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:

  • http://ww开发者_Go百科w.flexdeveloper.eu/forums/flex-4/drop-down-list-prompt/

  • Flex 4 Drop Down List - bound with data service, how to always keep prompt or add item at index -1 programmatically


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;
    }
}


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.


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

Receipt


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>


dropDownList.selectedItem=null

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜