[英]A Better Way To Make SelectedIndex Correspond To Selected Item In Flex ComboBox
[英]flex 4 combobox selectedIndex failure
有一個帶有數據提供程序的ComboBox,其中的n> 3個字符串值。
combobox.inputText.text = "value in dataprovider";
// OK, selectedIndex is set also
幾秒鍾后,由用戶按下按鈕啟動:
combobox.selectedIndex = 3;
// OK
幾秒鍾后再次執行此操作
combobox.inputText.text = "value NOT in dataprovider";
最后一行設置了combobox.inputText,但是盡管inputText值不在dataprovider值中,但將selectedIndex設置為3。
通過以下示例可以證明這一點,方法是先按按鈕1,再按按鈕4,再按按鈕1。
<?xml version="1.0" encoding="utf-8"?>
<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"
initialize="initializeHandler(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable] private var array : ArrayCollection;
protected function initializeHandler(event:FlexEvent):void {
array = new ArrayCollection();
array.addItem("0:00");
array.addItem("0:30");
array.addItem("1:00");
array.addItem("1:30");
addEventListener(Event.ENTER_FRAME, ef);
}
protected function btnSelect1_clickHandler(event:MouseEvent):void {
cb.selectedIndex = 3;
}
protected function btnSelect2_clickHandler(event:MouseEvent):void {
cb.selectedIndex = -1;
}
protected function btnSelect3_clickHandler(event:MouseEvent):void {
cb.textInput.text = "1:00";
}
protected function btnSelect4_clickHandler(event:MouseEvent):void {
cb.textInput.text = "1:01";
}
protected function ef(event:Event):void {
l.text = "inputText=\"" + cb.textInput.text + "\" selectedIndex=\""+cb.selectedIndex+"\"";
}
]]>
</fx:Script>
<s:VGroup>
<s:ComboBox id="cb" dataProvider="{array}"/>
<s:Button label="select index 3" click="btnSelect1_clickHandler(event)" />
<s:Button label="select index -1" click="btnSelect2_clickHandler(event)" />
<s:Button label="select '1:00'" click="btnSelect3_clickHandler(event)" />
<s:Button label="select '1:01'" click="btnSelect4_clickHandler(event)" />
<s:Label id="l" />
</s:VGroup>
</s:Application>
嘗試不要直接操縱inputText
。 而是使用getItemIndex
方法在ArrayCollection
查找對象。 請參閱上面重寫的函數:
protected function btnSelect3_clickHandler(event:MouseEvent):void {
cb.selectedIndex = array.getItemIndex("1:00");
}
protected function btnSelect4_clickHandler(event:MouseEvent):void {
cb.selectedIndex = array.getItemIndex("1:01");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.