jump to navigation

Sorting and filtering data in an XMLListCollection August 1, 2010

Posted by Tournas Dimitrios in Flex.
trackback

The following code is a brief example of sorting a Flex XMLListCollection using the Sort and SortField classes, and the “XMLListCollection.sort” property. We also look at filtering the XMLCollection using a custom filter function.

<m x:A pplication xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import mx.collections.SortField;
            import mx.collections.Sort;
            import mx.controls.*;

            private function init():void {
                describeTypeXML = describeType(DataGrid);
                factoryMethodsXLC.source = describeTypeXML.factory.method;
            }

            private function sortXLC():void {
                var nameSort:Sort = new Sort();
                nameSort.fields = [new SortField('@name', true)];

                factoryMethodsXLC.sort = nameSort;
                factoryMethodsXLC.refresh();
            }

            private function filterXLC():void {
                if (filterCh.selected) {
                    factoryMethodsXLC.filterFunction = declaredBy_filterFunc;
                    factoryMethodsXLC.refresh();
                } else {
                    factoryMethodsXLC.filterFunction = null;
                    factoryMethodsXLC.refresh();
                }
            }

            private function declaredBy_filterFunc(item:XML):Boolean {
                return item.@declaredBy == describeTypeXML.@name;
            }
        ]]>
    </mx:Script>

    <mx:XML id="describeTypeXML" />

    <mx:XMLListCollection id="factoryMethodsXLC" />

    <mx:VBox>
        <mx:DataGrid id="factoryMethodsGrid"
                dataProvider="{factoryMethodsXLC}"
                width="400"
                rowCount="7">
            <mx:columns>
                <mx:DataGridColumn dataField="@name" />
                <mx:DataGridColumn dataField="@returnType" />
                <mx:DataGridColumn dataField="@declaredBy" />
            </mx:columns>
        </mx:DataGrid>
        <mx:HBox width="100%">
            <mx:Button id="sortBtn"
                    label="Sort ({factoryMethodsGrid.dataProvider.length} items)"
                    click="sortXLC()" />
            <mx:Spacer width="100%" />
            <mx:CheckBox id="filterCh"
                    label="{describeTypeXML.@name} only"
                    click="filterXLC()" />
        </mx:HBox>
    </mx:VBox>

</m x:A pplication>

Hopefully it helps somebody out there.

Read also this article >> The Basics of XMLListCollection Class in Flex3

Comments»

1. The Basics of XMLListCollection Class in Flex3 « Tournas Dimitrios - August 1, 2010

[…] article :View Sorting & Filtering an XMLListCollection Example Possibly related posts: (automatically generated)QGraphicsScene used as a QAbstractItemView IIData […]


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