ComboBox 筛选 过滤

package skins.my
{	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	
	import spark.components.ComboBox;
	import spark.events.TextOperationEvent;
	
	public class ComboBoxFilter extends ComboBox
	{
		public function ComboBoxFilter()
		{
			super();
		}
		
		private var _dataSource:ArrayCollection;
		[Bindable]
		public function get dataSource():ArrayCollection{
			return _dataSource;
		}
		public function set dataSource(value:ArrayCollection):void{
			
			this._dataSource=value;
			if(_dataSource!=null&&_dataSource.length>0){
				this.dataProvider = new ArrayCollection(_dataSource.toArray());
			}
		}
		
		override protected function textInput_changeHandler(event:TextOperationEvent):void{
			if(_dataSource!=null&&_dataSource.length>0){
				var array:ArrayCollection = new ArrayCollection(dataSource.toArray());
				array.filterFunction = dataSourceFilterFunction;
				array.refresh();
				this.dataProvider = new ArrayCollection(array.toArray());
				
				if(this.dataProvider!=null&&this.dataProvider.length>0){
					super.textInput_changeHandler(event);
				}
			}
		}
		
		private function dataSourceFilterFunction(obj:Object):Boolean{
			var str:String="";
			if(labelField!=null){
				str=(obj[labelField]!=null?obj[labelField].toString():"");
			}else{
				str=obj.toString();
			}
			if(StringUtil.trim(this.textInput.text).length==0){
				return true;
			}else if(str.indexOf(StringUtil.trim(this.textInput.text))>-1){
				return true;
			}else{
				return false;
			}
		}
		
	}
}


调用代码如下:

<my:ComboBoxFilter id="combo2" width="200" labelField="cpb002" dataSource="{绑定集合}" focusIn="IME.enabled=true"/>


 

你可能感兴趣的:(function,String,object,null,Class,import)