The following example shows how you can filter an XMLListCollection collection by using regular expressions in Flex 4.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

For more information on the the API changes related to the removal of the “fx” prefix in Flex 4, see the “Dropping the Fx Prefix” page on opensource.adobe.com.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/01/filtering-an-xmllistcollection-using-regular-expressions/ -->
<s:Application name="XMLListCollection_filterFunction_RegExp_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        backgroundColor="white">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            private function textInput_change():void {
                if (textInput.text.length == 0) {
                    xmlListColl.filterFunction = null;
                } else {
                    xmlListColl.filterFunction = xmlListColl_filterFunc;
                }
                xmlListColl.refresh();
            }
 
            private function xmlListColl_filterFunc(item:Object):Boolean {
                if (textInput.text.length == 0) return true;
                var f:String = "ig";
                var wasRegExp:RegExp = new RegExp(textInput.text, f);
                var nowRegExp:RegExp = new RegExp(textInput.text, f);
                var wasMatch:Boolean = wasRegExp.test(item.@was);
                var nowMatch:Boolean = nowRegExp.test(item.@now);
 
                return (wasMatch || nowMatch);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <fx:XML id="nodesXML" source="nodes.xml" />
        <mx:XMLListCollection id="xmlListColl"
                source="{nodesXML.node}"
                filterFunction="xmlListColl_filterFunc" />
    </fx:Declarations>
 
    <s:controlBarContent>
        <mx:Form styleName="plain">
            <mx:FormItem label="Filter:">
                <s:TextInput id="textInput"
                        change="textInput_change();" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            verticalScrollPolicy="on"
            width="90%"
            rowCount="10">
        <mx:columns>
            <mx:DataGridColumn dataField="@was" headerText="Was:" />
            <mx:DataGridColumn dataField="@now" headerText="Now:" />
        </mx:columns>
    </mx:DataGrid>
 
</s:Application>

And the nodes.xml data file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/01/filtering-an-xmllistcollection-using-regular-expressions/ -->
<nodes>
    <node was="mx.components.ButtonBarButton" now="spark.components.ButtonBarButton" />
    <node was="mx.components.DataGroup" now="spark.components.DataGroup" />
    <node was="mx.components.DropDownList" now="spark.components.DropDownList" />
    <node was="mx.components.FxButton" now="spark.components.Button" />
    <node was="mx.components.FxButtonBar" now="spark.components.ButtonBar" />
    <!-- ... -->
</nodes>

View source is enabled in the following example.

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

3 Responses to Filtering an XMLListCollection using Regular Expressions

  1. Peter deHaan says:

    And with a SWF file, no less! Yay!
    Hopefully that helps a few people find the renamed classes a bit easier.

    Sincerely,
    Peter

  2. Peter deHaan says:

    Also, for more information on various new awesomeness in Flex Gumbo (post fx prefix removal), see my co-worker Joan Tan’s excellent blog entry: “The Fx prefix has been removed from Gumbo”.

    Peter

  3. flexmember says:

    really its good example..it helped me a lot..thank u

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree