package com
{
import flash.events.Event;
import mx.collections.ArrayCollection;
import mx.controls.ComboBox;
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.selectRange(tempstr.length,tempstr.length);
}
}
private function filterFunction(item:Object):Boolean{
return item[this.labelField].toString().indexOf(this.text)!=-1;
}
}
}
这是全部代码-----重点有几个,第一 this.textInput.selectRange(tempstr.length,tempstr.length);
这行代码是在sdk为4以上用得;
要是在sdk为3的时候用 this .textInput.setSelection(tempstr.length,tempstr.length);
这句话很重要: 输入文本框的文字显示不出来,因为系统默认将光标一直放在第一个位置,造成用户输入的文字被取消掉,所以在即使不需要补全是,也要进行textInput.setSelection的操作 。
第二:因为这句话return item[this.labelField].toString().indexOf(this.text)!=-1;
用这个 自定义组件的时候必须设置labelfield;
为什么我会这么蛋疼,明明有s:combobox的搜索组件了,为什么还弄这个,因为外国很多技术大牛写的东西不支持直接使用4的组件,他们写的东西都是一套的很不好改,所以只能在这方面下手
这是全部代码-----重点有几个,第一 this.textInput.selectRange(tempstr.length,tempstr.length);
这行代码是在sdk为4以上用得;
要是在sdk为3的时候用 this .textInput.setSelection(tempstr.length,tempstr.length);
这句话很重要: 输入文本框的文字显示不出来,因为系统默认将光标一直放在第一个位置,造成用户输入的文字被取消掉,所以在即使不需要补全是,也要进行textInput.setSelection的操作 。
第二:因为这句话return item[this.labelField].toString().indexOf(this.text)!=-1;
用这个 自定义组件的时候必须设置labelfield;
为什么我会这么蛋疼,明明有s:combobox的搜索组件了,为什么还弄这个,因为外国很多技术大牛写的东西不支持直接使用4的组件,他们写的东西都是一套的很不好改,所以只能在这方面下手