AS代码如下:
package com.dfmcsoft.test
{
imp
imp
imp
public class FilterComboBox extends ComboBox
{
public function FilterComboBox()
{
super();
this.editable=true;
}
override protected function textInput_changeHandler(event:Event):void{
super.textInput_changeHandler(event);
FilterByKey(event);
}
//过滤数据
private function FilterByKey(event:Event):void{
var tempDataProvider:ArrayCollection = this.dataProvider as ArrayCollection;
if (tempDataProvider == null) return;
this.dataProvider.filterFunction = filterFunction;
var tempstr:String = this.text;
if(tempDataProvider.refresh()){
this.dropdown.selectedIndex = -1;
this.dropdown.verticalScrollPosition = 0;
this.text = tempstr;
this.open();
this.textInput.setFocus();
this.textInput.setSelection(tempstr.length,tempstr.length);
}
}
private function filterFunction(item:Object):Boolean{
return item['label'].toString().indexOf(this.text)!=-1;
}
}
测试的MXML如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="com.dfmcsoft.test.* " fontSize="13">
<mx:scriptt>
<![CDATA[
imp
[Bindable]
public var cards:ArrayCollection = new ArrayCollection(
[ {label:"张一", da
{label:"张二", da
{label:"张三", da
{label:"李四一", da
{label:"李五一", da
{label:"王六二", da
]]>
</mx:scriptt>
<mx:Panel title="ComboBox Control Example"
height="75%" width="75%" layout="horizontal"
paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
<ns1:FilterComboBox x="57" y="130" dataProvider="{cards}"/>
</mx:Panel>
</mx:Application>
注意:数据源一定是集合数组类型的,我尝试用了<mx:XML>定义数据源,结果没好用!