ComboBox 筛选 过滤

[java]  view plain copy
  1. package skins.my  
  2. {     
  3.     import mx.collections.ArrayCollection;  
  4.     import mx.utils.StringUtil;  
  5.       
  6.     import spark.components.ComboBox;  
  7.     import spark.events.TextOperationEvent;  
  8.       
  9.     public class ComboBoxFilter extends ComboBox  
  10.     {  
  11.         public function ComboBoxFilter()  
  12.         {  
  13.             super();  
  14.         }  
  15.           
  16.         private var _dataSource:ArrayCollection;  
  17.         [Bindable]  
  18.         public function get dataSource():ArrayCollection{  
  19.             return _dataSource;  
  20.         }  
  21.         public function set dataSource(value:ArrayCollection):void{  
  22.               
  23.             this._dataSource=value;  
  24.             if(_dataSource!=null&&_dataSource.length>0){  
  25.                 this.dataProvider = new ArrayCollection(_dataSource.toArray());  
  26.             }  
  27.         }  
  28.           
  29.         override protected function textInput_changeHandler(event:TextOperationEvent):void{  
  30.             if(_dataSource!=null&&_dataSource.length>0){  
  31.                 var array:ArrayCollection = new ArrayCollection(dataSource.toArray());  
  32.                 array.filterFunction = dataSourceFilterFunction;  
  33.                 array.refresh();  
  34.                 this.dataProvider = new ArrayCollection(array.toArray());  
  35.                   
  36.                 if(this.dataProvider!=null&&this.dataProvider.length>0){  
  37.                     super.textInput_changeHandler(event);  
  38.                 }  
  39.             }  
  40.         }  
  41.           
  42.         private function dataSourceFilterFunction(obj:Object):Boolean{  
  43.             var str:String="";  
  44.             if(labelField!=null){  
  45.                 str=(obj[labelField]!=null?obj[labelField].toString():"");  
  46.             }else{  
  47.                 str=obj.toString();  
  48.             }  
  49.             if(StringUtil.trim(this.textInput.text).length==0){  
  50.                 return true;  
  51.             }else if(str.indexOf(StringUtil.trim(this.textInput.text))>-1){  
  52.                 return true;  
  53.             }else{  
  54.                 return false;  
  55.             }  
  56.         }  
  57.           
  58.     }  
  59. }  


调用代码如下:

[html]  view plain copy
  1. <my:ComboBoxFilter id="combo2" width="200" labelField="cpb002" dataSource="{绑定集合}" focusIn="IME.enabled=true"/>  

转载:http://blog.csdn.net/hellofr/article/details/7543017

你可能感兴趣的:(ComboBox 筛选 过滤)