04
Aug
07

Filtering an XMLListCollection using the filterFunction property and regular expressions

Here is another handy little tip that you see all over the web, how do I filter a data grid (or other list-based control) based on a user’s input? Or more specificially, how to I limit the items that show in a list based on what a user types.

Since I already created a simple XML document of Countries/States, I thought I’d create a basic form that lets a user type in the first few characters of a state name and have the DataGrid filter its results. You’ll also notice that I had to do some not-so-tricky E4X filtering to extract only the American state names.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function filterFunc(item:Object):Boolean {
                return item.text().match(new RegExp("^" + stateName.text, "i"));
            }
        ]]>
    </mx:Script>

    <mx:XML id="dp" source="countries_states.xml" format="e4x" />

    <mx:XMLListCollection id="xmlListColl" source="{dp.country.(@name == 'United States of America').state}" filterFunction="filterFunc" />

    <mx:VBox>
        <mx:HBox width="100%">
            <mx:Label text="Name:" />
            <mx:TextInput id="stateName" width="100%" change="xmlListColl.refresh()" />
        </mx:HBox>

        <mx:DataGrid id="dataGrid" dataProvider="{xmlListColl}">
            <mx:columns>
                <mx:DataGridColumn id="codeCol" dataField="@code" headerText="Abbr:" width="60" />
                <mx:DataGridColumn id="nameCol" dataField="*" headerText="Name:" width="240" />
            </mx:columns>
        </mx:DataGrid>
        <mx:Label text="Filtered: Showing {xmlListColl.length} record(s)" visible="{stateName.text.length > 0}" />
    </mx:VBox>

</mx:Application>

View source is enabled in the following example.


14 Responses to “Filtering an XMLListCollection using the filterFunction property and regular expressions”


  1. 1 joshspoon Aug 5th, 2007 at 12:39 pm

    I did a similar example on my site http://etc.joshspoon.com. But I didn’t do the refresh (change=”xmlListColl.refresh()” )as easily as you did, though there is more then one way to skin a Flex app.

    Keep up the great examples!

  2. 2 JabbyPanda May 8th, 2008 at 2:05 am

    The written filter function in this example has a serious limitation - it only filters items from top level of hierarchy in XMLListCollection

  3. 3 Arnold May 14th, 2008 at 7:40 am

    Hi Peter,

    I’m currently doing a filter function in an application. My understanding is that when you try to filter a specific dataField you can do this: “if (item.origin == “WA”)” “origin” is a specific column on a datagrid column. But what if I want to store that datafield column in a variable?

    for example: var custom_datafield:String = “origin” ;
    and do this: if (item.custom_datafield == “WA”) — I am not getting any returned records and it did not give me errors as well. I need your expertise on this.
    Are there any other way to do this?

    In case you are wondering why I need to store the dataField name into a variable is because I am creating a datagrid component that will accept any XML file using only one datagrid and one filter functionality.

    Thanks,
    Arnold

  4. 4 FrogMo May 15th, 2008 at 1:59 pm

    Is the fact that only the top level of hierarchy in the XMLListCollection is filtered using filterFunction a bug?

    Like JabbyPanda said that is a HUGE limitation. Is there somework around for this? Is there a way to assign a filterFunction on each level of the XMLListCollection hierarchy?

  5. 5 tdurant May 18th, 2008 at 12:13 am

    Ug! how could I do this in flex 2 its driving me crazy

  6. 6 peterh Jun 11th, 2008 at 8:14 am

    Seconding Frogmo’s request for a workaround to the filterFunction’s deficient filtering. I think they just used ListCollections on flat hierarchies and when they added the XML counterpart, they forgot expand the filterFunction’s functionality.

  7. 7 peterd Jun 11th, 2008 at 9:14 am

    FrogMo / peterh,

    Can one of you file a bug/enhancement request at http://bugs.adobe.com/flex/ so Adobe can track the issue? Also, if you post the bug number here, I’m sure a few people can vote on the issue.

    Thanks,
    Peter

  8. 8 RObert Oct 28th, 2008 at 8:34 am

    Has a bug report been filed for the top level filtering problem? I haven’t been able to find any information on it.

  9. 9 peterd Oct 28th, 2008 at 9:11 am

    RObert,

    Can you please file a bug on the top level filtering problem at http://bugs.adobe.com/flex/ and include a simple test case, if possible.

    Thanks,
    Peter

  10. 10 RObert Oct 28th, 2008 at 1:14 pm
  11. 11 peterd Oct 28th, 2008 at 2:00 pm

    Voted and subscribed.

    Thanks,
    Peter

  12. 12 Elvis Fernandes Dec 23rd, 2008 at 8:46 am

    Great tip!!!! It helped me a LOT!

    Thanx!

    Elvis Fernandes

  13. 13 Peter Jun 1st, 2009 at 8:58 am

    From one Peter to another, thanks. However, I keep getting an error, “TypeError: Error #1006: value is not a function.” I’m not sure why this pops up. I originally tried applying this code to a working filterFunction that matched characters exactly, but when that didn’t work, I recreated an app from scratch, following the example above, but I still get the “…not a function” error. Any clues why this would show up?

  14. 14 Peter deHaan Jun 4th, 2009 at 8:19 pm

    Peter,

    Which version of the Flex SDK are you using?
    I just tried on 3.4 and didn’t have any errors.

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed