I have a ComboBox and the data provider is an ArrayCollection of 3 values: CA - California, NY - New York, TX - Texas. With the default behavior when I start typing in the ComboBox it will try to match the value from the beginning of the string, so if I start Typing TX it will bring up TX - Texas.
I want to be able to search at any part of the string and not just from the beginning, so if I type "xas" it will filter out the selection and only show TX - Texas. There is a very helpful post in the Adobe forums here on how to do this by changing the filter function on the ArrayCollection which provides data for the ComboBox and I have adapted it but I am having a slight issue with it.
If a user selects a value and then tries to enter in new text the first letter typed in the ComboBox does not show up.
1) Select the value CA - California in the ComboBox
2) Highlight the text and hit "n" on your keyboard
3) You would expect to see the textbox populated with "n" but the textbox remains empty
What could be causing this issue? It only happens if you already have a value selected. If you start with a blank ComboBox it works as expected.
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import spark.events.TextOperationEvent;
[Bindable]
public var arrC:ArrayCollection = new ArrayCollection([{label:'CA - California'},{label:'NY - New York'},{label:'TX - Texas'}]);
private function changeHandler(e:*):void
{
if (arrC.filterFunction != doFilter)
arrC.filterFunction = doFilter;
arrC.refresh();
}
private function doFilter(item:Object):Boolean
{
if(String(item.label).toLowerCase().indexOf(cb.textInput.text.slice(0 ,cb.textInput.selectionAnchorPosition).toLowerCase())>-1)
{
return true;
}
return false;
}
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
cb.textInput.addEventListener(TextOperationEvent.CHANGE,changeHandler );
}
]]>
</fx:Script>
<s:ComboBox id="cb" dataProvider="{arrC}"/>