组合框,可根据用户输入过滤数据提供者 [英] combobox which filters dataprovider based on user input

查看:79
本文介绍了组合框,可根据用户输入过滤数据提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个可根据用户输入过滤dataprovider的组合框吗?

I need a combobox which filters dataprovider based on user input?

例如,如果用户输入为"CA",则组合框将过滤数据提供程序并仅显示以"CA"开头的数据. (加拿大"和加利福尼亚")

For example if user input is "CA" combobox will filter the dataprovider and show only data which starts with "CA". ("CANADA" and "CALIFORNİA")

注意:组合框是可编辑的.

note:combobox is editable.

并且ı需要在flex 3中使用此组合框.

and ı need this combobox in flex 3.

edit:好的,我从javaluca那里得到了一些帮助,我写了这个.

edit:okay ı got some help from javaluca and ı wrote this.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    creationComplete="createCompFonk()">
    <mx:Script>
     <![CDATA[
        import mx.controls.Text;
        import mx.controls.dataGridClasses.DataGridColumn;
     import mx.controls.TextInput;
 import mx.events.ItemClickEvent;
 import mx.events.DataGridEvent;
 import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.collections.ListCollectionView; 
import mx.controls.ComboBox

     var sayac:int=0;
     var degisken:String;

     [Bindable]
     private var initDG:ArrayCollection = new ArrayCollection([ {islem:'Tahsilat112', tarih:'01/01/2000', isim:'ATA'},{islem:'Ödeme', tarih:'02/03/2010', isim:'BETA'}      ]);




      public function createCompFonk():void{
        gggcombo.textInput.addEventListener(TextEvent.TEXT_INPUT, textChange);

      }
    protected function textChange(evt:TextEvent):void{
    (gggcombo.dataProvider as ArrayCollection).filterFunction = filterFunc;
    (gggcombo.dataProvider as ArrayCollection).refresh();
    // REFRESH IS NECESSARY
}

public function filterFunc(item:Object):Boolean{
    var pattern:String = gggcombo.textInput.text.toLowerCase();
    var string:String = item.islem;

    return pattern == string.substr( 0, pattern.length );
}
        ]]>
      </mx:Script>


  <mx:Panel x="0" y="0" width="435" height="216" 
      layout="absolute" title="Empty Default Datagrid">


   <mx:ComboBox id="gggcombo" dataProvider="{initDG}" labelField="islem" editable="true"  >


   </mx:ComboBox>
  </mx:Panel>


</mx:Application>

但是我在这些行上有一个错误:

but ı got an error on these rows :

gggcombo.textInput.addEventListener(TextEvent.TEXT_INPUT, textChange);

 var pattern:String = gggcombo.textInput.text.toLowerCase();

错误是: 通过静态类型为mx.controls:comboBox

Error is : Attempted acces of inaccesible property textInput through a reference with static type mx.controls:comboBox

推荐答案

如果将ArrayCollection类用于组合框数据提供程序,则可以使用 filterFunction .

If you are using ArrayCollection Class for combobox dataprovider, you can use the filterFunction.

在组合中包含的textInput上添加侦听器

Add listener on textInput that is contained into the combo

myCombo.textInput.addEventListener(TextEvent.TEXT_INPUT, textChange);

在textChange上分派的功能

Function dispatched on the textChange

protected function textChange(evt:TextEvent):void{
    (myCombo.dataProvider as ArrayCollection).filterFunction = filterFunc;
    (myCombo.dataProvider as ArrayCollection).refresh();
    // REFRESH IS NECESSARY
}

用于过滤dataProvider的FilterFunction

FilterFunction for filtering dataProvider

public function filterFunc(item:Object):Boolean{
    var pattern:String = myCombo.textInput.text.toLowerCase();
    var string:String = item.name;

    return pattern == string.substr( 0, pattern.length );
}

这篇关于组合框,可根据用户输入过滤数据提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆