一个带过滤功能的combobox组件

 

AS代码如下:

package com.dfmcsoft.test
{
   
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.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[
            import mx.collections.ArrayCollection;
            [Bindable]
            public var cards:ArrayCollection = new ArrayCollection(
                [ {label:"张一", data:1},
                  {label:"张二", data:2},
                  {label:"张三", data:3},
                  {label:"李四一", data:1},
                  {label:"李五一", data:1},
                  {label:"王六二", data:1} ]);   
        ]]>
    </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>定义数据源,结果没好用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用法举例:从数据表中name字段写入VALComboBox的items,id字段写入VALComboBox的values,当从VALComboBox选择一个选项后,就可以从value属性获得相应的ID值,或者写value的值为某ID值,VALComboBox将定位在相应的选项上;unit VALComboBox;interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Menus, Dialogs, StdCtrls;type TValComboBox = class(TComboBox) private FValue: PString; FValues: TStrings; FOnChange: TNotifyEvent; function GetValue: string; function GetButtonValue(Index: Integer): string; procedure SetValue(const Value: string); procedure SetValues(Value: TStrings); protected procedure Change; dynamic; procedure Notification(AComponent: TComponent; Operation: TOperation); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property Value: string read GetValue write SetValue; property ItemIndex; published property Values: TStrings read FValues write SetValues; property OnChange: TNotifyEvent read FOnChange write FOnChange; end;procedure Register;implementationconstructor TValComboBox.Create(AOwner: TComponent);begin inherited Create(AOwner); FValue := NullStr; FValues := TStringList.Create; style := csDropDownList;end;destructor TValComboBox.Destroy;begin DisposeStr (FValue); FValues.Free; inherited Destroy;end;procedure TValComboBox.Notification(AComponent: TComponent; Operation: TOperation);begin inherited Notification(AComponent, Operation);end;function TValComboBox.GetValue : string;begin result:=values[itemindex];end;function TValComboBox.GetButtonValue(Index: Integer): string;begin if (Index < FValues.Count) and (FValues[Index] ‘‘) then Result := FValues[Index] else if (Index < Items.Count) then Result := Items[Index] else Result := ‘‘;end;procedure TValComboBox.SetValue (const Value: string);var I : Integer;begin AssignStr(FValue, Value); if (ItemIndex < 0) or (GetButtonValue(ItemIndex) Value) then begin if (ItemIndex >= 0) then ItemIndex := -1; for I := 0 to Items.Count - 1 do begin if GetButtonValue(I) = Value then begin ItemIndex := I; break; end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值