Filter Data with ArrayCollection

June 6th, 2007

After watching a Lynda.com tutorial realized I didn’t have to do as much work to filter data.

the filterFunction is a public property of the ICollectionView witch is the class that the ArrayCollection extends from.

So this function/method is accessible to a ArrayCollection.
[as]

< ?xml version="1.0" encoding="utf-8"?>

< ![CDATA[
import mx.collections.*;

private var collectionArray:Array;
[Bindable]
private var collectionData:ArrayCollection;

private function init():void
{
collectionArray = [{first: 'Dave', last: 'Matthews'},
{first: 'Amy', last: 'Grant'},
{first: 'Bilbo', last: 'Baggins'},
{first: 'Jessica', last: 'Tandy'},
{first: 'Paris', last: 'Hilton'}];
collectionData = new ArrayCollection(collectionArray);
}

public function filter():void {

//pass my filter function to the method w/ no ()
// you must also refresh it
collectionData.filterFunction = filterFirst;
collectionData.refresh();

}
// function see's if the searchField's text is equal to the "first" object of the collectionData

//if true it show results, if false it shows nothing
private function filterFirst(item:Object):Boolean
{
return (item.first == searchField.text)

}
]]>
[/as]

Problems w/ this:

  1. I have not found a way to match with just typing in “Da” and it is case-sensitive
  2. When you type in a non-matching string it will not reset to beginning state. (I’m sleepy so maybe that’s why I can’t figure it out.)

Last 5 posts in actionscript

Last 5 posts in as3

Last 5 posts in flash

Last 5 posts in Flex

Entry Filed under: Flex, actionscript, as3, flash

3 Comments Add your own

  • 1. Nolan  |  July 24th, 2007 at 8:45 am

    Try looking into Regular Expressions, which make it easy to have case-insensitive searches with fragments of the word.

    return (item.first == searchField.text)

    becomes:

    return item.first.match(new RegExp(searchFiel.text, “i”))

    The second parameter tells the RegEx to ignore case sensitivity.

  • 2. joshspoon  |  July 25th, 2007 at 2:28 pm

    Thanks I’m working on finding that out right now

  • 3. Kausar Khan  |  January 25th, 2008 at 2:47 pm

    Usefull info what I was searching for. Thank you again.

Leave a Comment

Required

Required, hidden

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Trackback this post  |  Subscribe to the comments via RSS Feed