Using Regular Expressions to Filter Arraycollections.

Consider we have a requirement like filtering the DataGrid based on some inputs.  Like consider I have a DataGrid and have a DataGridColumn which contains Places list (Bangalore,Delhi,Tirupathi,Chennai…ets).Now that I want to filter this DataGrid upon Places DataGridColumn, based on some TextIput entry.so how am I gonna do this ??.

Here I will show two cases of filtering using the ArrayCollection with RegularExpressions.

I have a ArrayCollection With the Data Like below.

<mx:ArrayCollection id=”prods”>
<mx:Object Artist=”Pavement object killing” Price=”11.99″
Album=”Slanted and Enchanted” />
<mx:Object Artist=”kavement regular expression”
Album=”Brighten the Corners” Price=”11.99″ />
<mx:Object Artist=”Pavement hrule radio” Price=”11.99″
Album=”Slanted and Enchanted” />
<mx:Object Artist=”kavement”
Album=”Brighten the Corners” Price=”11.99″ />
<mx:Object Artist=”Pavement button checkbox” Price=”11.99″
Album=”Slanted and Enchanted” />
<mx:Object Artist=”kavement may be this is”
Album=”Brighten the Corners” Price=”11.99″ />
</mx:ArrayCollection>

Now I want to filter the Artist Column. And  I want 2 cases for filtering or conditions for filtering.

Case 1: The entered text should occur any where in the column data.

For this my Regular Expression would be like below.

new RegExp(sinput.text,”i”)

Here sinput is the id of my TextInput.

Case 2: The column data should start with the entered text.

For this my Regular Expression would be like below.

var strPatt:String=”^”+sinput.text+””;
var newRegEx:RegExp=new RegExp(strPatt,”i”);

Here sinput is the id of my TextInput.

Explore RegularExpression to learn more about them or how to write one.And the best source would the Help from the Flex Builder. And Explore about writing Filter Functions For Arraycollection.

Now Enjoy this Demo Application.

Comment For queries or Code. Enoy the Post. 🙂

Updated:

DownLoad source code

Advertisements

10 Responses to Using Regular Expressions to Filter Arraycollections.

  1. harish says:

    HI,
    Thanks for providing above application
    please can u give me the code…….

  2. kumargandhi says:

    hi i will mail you ASAP.

    regards,
    kumar.

  3. Marius says:

    Thanks for providing above application
    Would you please send me the code.

    Thanks in advance

    Best regards and happy new year
    Marius

  4. David says:

    Thanks.
    Please send me the code.

  5. handoyo says:

    Thanks a lot for the code…

  6. Lea-Mueller says:

    I cannot believe this will work!

  7. Bheeman says:

    Hi kumar,
    This is bheeman
    i have a problem in filter function actually i am fetching data from DB and in the result event am storing it in an array collection …
    am using the code as below..

    private function empDetailsRes(event:ResultEvent):void
    {
    empArray=event.result.employee.details;
    empArray.filterFunction=empInfoFilter;

    }

    private function empInfoFilter(item:Object):Boolean
    {
    if(cmbId.selectedItem.SELID==3)
    {
    var f:String=”ig”;
    //var tmpName:String=new String(item.name);
    var orgExp:RegExp=new RegExp(txtSearch.text,f);
    var orgMatch:Boolean= orgExp.test(item.name);
    return(orgMatch);
    }

    private function setNewVal():void
    {

    empArray.refresh();
    }

    am able to search but i want the result in some different manner ..assume if ihave got four names
    rakesh
    ramesh kumar
    deepa raman
    ricky rajess

    if i type in ra in text box i should get
    rakesh
    ramesh kumar
    ricky rajess
    deepa raman

    i mean first i need to get all the names with the letter i typed in and then i need to get the remaining names which are getting matched…

    plz help me..:) i will be thank full to you..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: