Flex:当数据提供程序更新时,Combobox失败是关注的焦点吗?

时间:2009-05-01 13:35:02

标签: flex

似乎ComboBoxes在dataProvider更新后松开了所选项目,即使相同的选定项目仍在dataProvider中。它们会转换回所选的第一个项目。反正有没有阻止这个?因此,如果同一个对象在dataProvider中,它会保持选择相同的对象,并且只有在所选对象不在更新的dataProvider中时才会恢复到第一个索引?

谢谢!

2 个答案:

答案 0 :(得分:1)

如果ComboBox丢失其选定的项目,则表示dataProvider未更新 - 它将被替换。如果将ComboBox绑定到ArrayCollection然后将项添加到AC,则会更新ComboBox而不会丢失其selectedItem。

有时您必须替换dataProvider,在这种情况下,您必须侦听updateComplete事件并重置selectedItem。您可以尝试以下代码:

<mx:Script>
    <![CDATA[
        import mx.controls.ComboBox;
        import mx.events.ListEvent;
        import mx.events.FlexEvent;
        import mx.collections.ArrayCollection;

        [Bindable]
        private var dp:ArrayCollection = new ArrayCollection(["Item 1", "Item 2", "Item 3"]);

        private var selectedItem:*;
        private var dataProvider:*;

        private function onChange(event:ListEvent):void {
            selectedItem = (event.currentTarget as ComboBox).selectedItem;
        }
        private function onUpdateComplete(event:FlexEvent):void {
            trace(event);
            var cb:ComboBox = event.currentTarget as ComboBox;
            if(dataProvider == null || cb.dataProvider != dataProvider) {
                if(selectedItem != null && cb.selectedItem != selectedItem) cb.selectedItem = selectedItem;
                if(cb.selectedIndex < 0) cb.selectedIndex = 0;
                dataProvider = cb.dataProvider;
            }
        }

        private function extendDP():void {
            dp.addItem("Item " + (dp.length +1));
            var ac:ArrayCollection = new ArrayCollection(dp.source);
            dp = ac;
        }

        private function reduceDP():void {
            dp.removeItemAt(dp.length -1);
            var ac:ArrayCollection = new ArrayCollection(dp.source);
            dp = ac;
        }
    ]]>
</mx:Script>

<mx:VBox>
    <mx:ComboBox dataProvider="{dp}" change="onChange(event)" updateComplete="onUpdateComplete(event)" />
    <mx:Button label="Extend dp" click="extendDP()" />
    <mx:Button label="Reduce dp" click="reduceDP()" />
</mx:VBox>

它创建一个ComboBox并将其绑定到ArrayCollection。这两个按钮可以添加和删除集合中的项目。

答案 1 :(得分:0)

我能够使用此类扩展ComboBox,它只查找所选标签并与新dataProvider中的标签进行比较。它似乎有效,虽然有点特别。我希望有一个可扩展的解决方案。

package
{
    import mx.controls.ComboBox;
    import mx.collections.ArrayCollection;

    public class SelectionKeepingComboBox extends ComboBox
    {
        public function SelectionKeepingComboBox()
        {
            super();
        }

        override public function set dataProvider(value:Object):void
        {
            var curSelectedLabel:String;
            if(this.selectedItem)
            {
                curSelectedLabel = this.selectedLabel;
            }

            super.dataProvider = value;

            if(curSelectedLabel == null)
            {
                return;
            }

            var dp:Array;
            if(this.dataProvider is ArrayCollection)
            {
                dp = this.dataProvider.toArray();
            }
            else
            {
                dp = this.dataProvider as Array;
            }
            for(var i:uint = 0; i<dp.length; i++)
            {
                var obj:Object = dp[i];
                var dpLabel:String = obj.label;
                if(dpLabel == curSelectedLabel)
                {
                    this.selectedItem = obj;
                }   
            }
        }

    }
}