Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / ArrayCollection Filter Example

ArrayCollection Filter Example

17 November 2009

Line Break

Author: Roelof (13 Articles) - Author Website

Roelof is a SAP Consultant specialized in Front-End development. In his spare free time he is either developing on the web, playing basketball, watching soccer or traveling. He is also the co-owner of Flex-Blog.com.


This example is all about filtering an ArrayCollection in a DataGrid. We will filter the ArrayCollection using the input from the TextInput field, the value entered as the datagrid values are translated to lower case.

The search is performed without position, this means that if I type ‘weaver’ in the search box, i will find ‘Dreamweaver’ in my results.

Here is the example: (Source can be viewed underneath the example or via Right Mouse Click -> View Source ).

The (almost) complete source code is right here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/halo"
               initialize="init()"
               width="500" height="300" viewSourceURL="srcview/index.html">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
   
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
           
            [Bindable]
            private var myArrayCollection:ArrayCollection;
           
            private var firstTime:Boolean = true;
           
            private function init():void {
                myArrayCollection = new ArrayCollection;
                var obj:Object = new Object;
               
                obj.product = "Photoshop CS4";
                obj.instock = "4";
                myArrayCollection.addItem(obj);
                obj = new Object;
                obj.product = "Dreamweaver CS3";
                obj.instock = "8";
                myArrayCollection.addItem(obj);
                //etc
            }
           
            private function filter():void {
                myArrayCollection.filterFunction = filterMyArrayCollection;
                myArrayCollection.refresh();
            }
           
            private function filterMyArrayCollection(item:Object):Boolean {
                var searchString:String = myTextInput.text.toLowerCase();
                var itemName:String = (item.product as String).toLowerCase();
                return itemName.indexOf(searchString) > -1;
            }
           
            private function clearMyTextInput():void {
               
                if (firstTime == true )
                {
                    myTextInput.text = "";
                    firstTime = false;
                }
            }
           
        ]]>
    </fx:Script>
   
    <mx:VBox right="10" left="10" bottom="10" top="10">
        <s:TextInput id="myTextInput" change="filter()"
                     enabled="true"
                     focusIn="clearMyTextInput()"
                     text="Filter/Search.."
                     width="100%" height="26"/>
       
        <mx:DataGrid id="dataGrid"
                     dataProvider="{myArrayCollection}"
                     verticalScrollPolicy="on" dropShadowVisible="false" verticalAlign="top" editable="false" width="100%" height="80%" paddingTop="2">
            <mx:columns>
                <mx:DataGridColumn dataField="product"  headerText="Product"/>
                <mx:DataGridColumn dataField="instock"  headerText="In Stock"/>
            </mx:columns>
        </mx:DataGrid>
    </mx:VBox>

</s:Application>

Related posts:

  1. Looping over an ArrayCollection with Cursor Example
  2. Style AdvancedDataGrid depending on data example
  3. Progressbar in Datagrid Example
  4. Images in DataGrid depending on data
  5. Drag and Drop from DataGrid or AdvancedDataGrid to Tree

Written by Roelof · Filed Under Examples 

Was this post useful to you?

Please rate this post, follow us @ twitter, or link to this page from your website!

1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.70 out of 5)
Loading ... Loading ...

757f179cea41c86bd3aa04047f33531cdelicious

Comments

8 Responses to “ArrayCollection Filter Example”

  1. ajai robin on June 11th, 2010 8:42 am

    Thanks Roelof…. the post was really helpfull….

  2. Roelof on July 5th, 2010 3:28 am

    hi, this is very helpfull. thanks for the wonderful post.

  3. Devtron on September 9th, 2010 5:50 pm

    Hello,

    What happens if you have a NULL value in product?

    I am experiencing this and am wondering how to workaround it?

  4. Devtron on September 9th, 2010 9:16 pm

    I found my solution.

    In my data-access method, I must translate any NULL values to empty strings. This forces the regular expression search to work for me.

    Also, I wanted to know how to reset the filter textbox’s default text. Here is how I approached it:

    private function resetMyTextInput():void {
    if (myTextInput.text.length==0)
    myTextInput.text = “Filter/Search..”;
    }

    in MXML, add this to your TextInput. and it re-display the user message for filtering.
    focusOut=”resetMyTextInput()”

  5. Roelof on December 14th, 2010 10:34 am

    @Devtron: nice addition!

  6. Krish on April 28th, 2011 9:13 am

    Hi
    Thank you so much for this example. I wonder how to hookup to an products xml file? Can you help me please?
    Thank You
    Krish

  7. Jim on May 7th, 2011 7:02 pm

    I was wondering, how can I search not only on product item but also on stock item?

  8. Roelof on May 9th, 2011 9:12 am

    Hi Jim,

    I have no flash builder here, but you should extend the filterfunction. Just make sure the method returns a boolean value.

    If you don’t get it working, you can try our forum!

    Roelof

Get Adobe Flash player

  • +1?

  • Support Flex Blog!

  • $ 13 raised
    • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
    • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
    • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
  • Stay in touch!

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • FileStream
    • Flash Builder
    • Flash Builder 4
    • Flex 4
    • Flex Mobile
    • Framework
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • Mobile
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
  • Advertisements

  • Recent Posts

    • Spooky Frenzy – iPad Game
    • Fountain Example
    • Reading & Writing files in Adobe AIR
    • CheckBox in List using MobileIconItemRenderer for Flex Mobile
    • Data Dependent decoratorClass in MobileIconItemRenderer Example
    • Flex 4 Resize Effect Example
    • Jump to next field using the Focus Manager
    • Searching Data using a Class Example
    • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
    • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  • Categories

    • Examples
    • Guest Poster
    • iOS
  • Archives

    • September 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • March 2009
    • February 2009
  • Blogroll

    • Adobe Flex Jobs
    • NL for Business
  • Meta

    • Register
    • Log in
    • WordPress
    • XHTML

Copyright © 2010 Flex Blog · AdobeĀ® and AdobeĀ® Flex are registered trademarks of Adobe Systems.

WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

  • Popular Posts

    • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
    • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)