将“New item Label”添加到Spark ComboBox

时间:2011-07-15 11:51:09

标签: flex data-binding combobox custom-component flex-spark

我有一个Custom Spark ComboBox,其中包含一个项目列表。我想在ComboBox中添加一个新项目,说明文本“添加新项目”,选择弹出窗口进行一些操作。

我通过在数据提供程序中创建相同类型实体的新对象来实现此目的,新对象的LabelField具有文本“添加新项”。我已经覆盖了set dataProvider方法并在自定义组合框中。

但是这会将值添加到绑定到DataProvider的实际列表中。该列表用于业务逻辑。所以我不希望这种情况发生。我有很多实体类。我无法改变所有物体。

我想要的只是在我的自定义组件中实现相同的功能而不更改其他代码。我还尝试创建一个新的dataProvier实例,但我注意到当我创建一个新实例时,List和dataprovider的绑定会丢失。

请帮助。!!

编辑:

ExtendedComboBox.as

package components

{     import flash.utils.getDefinitionByName;

import mx.collections.ArrayCollection;
import mx.collections.IList;

import spark.components.ComboBox;
import spark.events.DropDownEvent;

public class ExtendedComboBox extends ComboBox
{

    private var _addItem:Boolean = false;
    private var _addItemLabel:String = "Create New Item" ;
    private var _dropDownClass:String = null ;
    private var originalDP:IList ;
    private var dpEdited:Boolean = false;

    public function ExtendedComboBox()
    {
        super();
        this.addItem = true;
        this.addEventListener(DropDownEvent.CLOSE, dropDownCloseEventListner );
        this.addEventListener(DropDownEvent.OPEN, openDropDownEvent );
    }

    public function get dropDownClass():String
    {
        return _dropDownClass;
    }

    public function set dropDownClass(value:String):void
    {
        _dropDownClass = value;
    }

    public function get addItemLabel():String
    {
        return _addItemLabel;
    }

    public function set addItemLabel(value:String):void
    {
        _addItemLabel = value;
    }

    public function get addItem():Boolean
    {
        return _addItem;
    }

    public function set addItem(value:Boolean):void
    {
        _addItem = value;
    }

    private function dropDownCloseEventListner(event:DropDownEvent):void{

    }

    protected function openDropDownEvent(event:DropDownEvent):void{
        if(addItem)
        {
            //  if(value) value = new ArrayCollection();
            var value:IList  ; 
            if(originalDP == null)  value = new ArrayCollection ;
            else value = new ArrayCollection( originalDP.toArray() ) ;
            var tempObj:Object;
            var createItemPresent:Boolean =false ;
            if(dropDownClass != null)           
            {
                var TempClass = flash.utils.getDefinitionByName(dropDownClass) as Class;
                tempObj = new TempClass();
                if(value.length >0)
                {
                //  trace(value.getChildAt(0)[this.labelField]) ;
                    if(value.getItemAt(0)[this.labelField] == addItemLabel)
                        createItemPresent = true ;
                }
                if(!createItemPresent)
                {
                    tempObj[this.labelField] = addItemLabel ;
                    var sort = (value as ArrayCollection).sort ;
                    value.addItemAt(tempObj, 0);
                    (value as ArrayCollection).sort = sort ;
                    dpEdited = true;
                }
            }
        }
        super.dataProvider = value;
    }

    override public function set dataProvider(value:IList):void{
        if(!dpEdited)
        {
            originalDP = value;
            dpEdited = true;
        }

        /*if(addItem)
        {
        //  if(value) value = new ArrayCollection();
            var tempObj:Object;
            var createItemPresent:Boolean =false ;
            if(dropDownClass != null)           
            {
                var TempClass = flash.utils.getDefinitionByName(dropDownClass) as Class;
                tempObj = new TempClass();
                if(value.length >0)
                {
                    if(value.getItemIndex(0)[this.labelField] == addItemLabel)
                        createItemPresent = true ;
                }
                if(!createItemPresent)
                {
                tempObj[this.labelField] = addItemLabel ;
                var sort = (value as ArrayCollection).sort ;
                value.addItemAt(tempObj, 0);
                (value as ArrayCollection).sort = sort ;
                }
            }
        }*/
        super.dataProvider = value;
    }
}

}

MyEntityObj.as

package entity

{     公共类MyEntityObj     {         private var _name:String;         private var _age:int;         private var _company:String;

    public function MyEntityObj()
    {
    }

    public function get company():String
    {
        return _company;
    }

    public function set company(value:String):void
    {
        _company = value;
    }

    public function get age():int
    {
        return _age;
    }

    public function set age(value:int):void
    {
        _age = value;
    }

    public function get name():String
    {
        return _name;
    }

    public function set name(value:String):void
    {
        _name = value;
    }

}

}

和实施示例 - ComboImpl.mxml

<?xml version="1.0" encoding="utf-8"?>

             

        import mx.collections.ArrayCollection;
        import mx.events.CollectionEvent;
        import mx.events.FlexEvent;

        [Bindable]
        private var samplDP:ArrayCollection;

        protected function application1_initializeHandler(event:FlexEvent):void
        {
            samplDP = new ArrayCollection ;
            samplDP.addEventListener(CollectionEvent.COLLECTION_CHANGE, changeHandlerFunc );
            var sampVO:MyEntityObj;
            for(var i:int = 0; i<5;i++)
            {
                sampVO = new MyEntityObj;
                sampVO.name = "Name " + i;
                sampVO.age = i;
                sampVO.company = "Company " + i;
                samplDP.addItem(sampVO);
            }
        }

        protected function changeHandlerFunc(event:CollectionEvent):void{
            var nameList:String = "" ;
            for each(var myObj:* in samplDP)
            {
                nameList += myObj.name + ", " ;
            }
            changeHandler.text = nameList ;
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:VGroup>
    <s:Label id="changeHandler" />  
    <components:ExtendedComboBox dataProvider="{samplDP}" labelField="name" addItem="true" dropDownClass="entity.MyEntityObj" addItemLabel="Create Sample" />
</s:VGroup>

如在设置DataProvider Overridden方法和DropDownClose事件中所评论的那样,新项目的添加直接影响原始List。我不希望这种情况发生。

请注意,它只是一个示例实现。我的项目中的组件创建实际上是在动作脚本类中动态发生的。

请帮助。!!

1 个答案:

答案 0 :(得分:0)

听起来像是:

你将不得不扩展ComboBox(如果深入研究它的实现方式,可能是DataGroup),以包含你在dataProvider中的额外“Add New Item”项。

我希望这很困难,但是没有为此目的审查代码。