【问题标题】:Spark ComboBox - issue with displaying text for new item?Spark ComboBox - 显示新项目文本的问题?
【发布时间】:2010-09-16 23:06:57
【问题描述】:

我一直在玩 s:ComboBox 并且通常非常喜欢它们。一个细节让我发疯 - 很可能是由于我缺乏该主题的知识 - 如果我尝试在 changeHandler (注册到更改事件)中向我的数据提供者添加一个新项目,则 ComboBox textInput 的文本消失- 虽然项目添加完美。有趣的是,如果通过单击按钮调用相同的操作,即发布更改事件已被处理,并且文本不会消失,则它可以正常工作。这是来自here 的一些代码,显示了这一点

<?xml version="1.0" encoding="utf-8"?>
<!-- dpcontrols\spark\SparkCBAddItem.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx">
 <s:layout>
  <s:VerticalLayout paddingTop="5" paddingLeft="5"/>
 </s:layout> 

 <fx:Script>
  <![CDATA[
   import mx.collections.ArrayList;

   import spark.events.IndexChangeEvent;

   [Bindable]
   public var myDP:ArrayList = new ArrayList(["Red", "Orange", "Yellow", "Blue", "Green"]);

   // Event handler to determine if the selected item is new.
   protected function myCB_changeHandler(event:IndexChangeEvent):void
   {
    // Determine if the index specifies a new data item.
    if(myCB.selectedIndex == spark.components.ComboBox.CUSTOM_SELECTED_ITEM)
    {
     // Add the new item to the data provider.
     myCB.dataProvider.addItem(myCB.selectedItem);
    }
   }

   protected function button1_clickHandler(event:MouseEvent):void
   {
    // Determine if the index specifies a new data item.
    if(myCB.selectedIndex == spark.components.ComboBox.CUSTOM_SELECTED_ITEM)
    {
     // Add the new item to the data provider.
     myCB.dataProvider.addItem(myCB.selectedItem);
    }
   }

  ]]>
 </fx:Script>

 <s:Label text="The selected index is: {myCB.selectedIndex}"/>
 <s:Label text="The selected item is: {myCB.selectedItem}"/>

 <s:ComboBox id="myCB" width="140" change="myCB_changeHandler(event);" dataProvider="{myDP}"/> 
 <s:Button label="Button" click="button1_clickHandler(event)"/>

</s:Application>  

如果您取出更改处理程序,您将看到添加一个新项目,然后单击按钮将新元素保留在 textInput 中,同时将项目添加到数据提供程序,如果您只是输入一个新的,则不会发生这种情况项目,然后按 Enter。

提前感谢您的帮助!

【问题讨论】:

  • 即使你会用输入事件来处理它?我认为那一刻 CUSTOM_SELECTED_ITEM 有问题,但是您稍后调用的想法看起来足够好用,但是。告诉我。

标签: apache-flex flex4


【解决方案1】:

好的,我猜 myCB_changeHandler 的内容上的 callLater 似乎可以解决问题,即

private function later(event:IndexChangeEvent):void
{
    // Determine if the index specifies a new data item.
    if(myCB.selectedIndex == spark.components.ComboBox.CUSTOM_SELECTED_ITEM)
    {
        // Add the new item to the data provider.
        myCB.dataProvider.addItem(myCB.selectedItem);
    }

}

// Event handler to determine if the selected item is new.
protected function myCB_changeHandler(event:IndexChangeEvent):void
{
      callLater(later, [event]);
}

【讨论】:

  • 为我工作。我在更改事件上调用了一些代码并将焦点设置在另一个组件上,它会使下拉菜单保持打开状态。稍后使用调用它就像一个魅力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
相关资源
最近更新 更多