xmlactionscript-3airflash-builderfilterfunction

Creating a List based on single XML elements in Flash Builder from XMLListCollection filterFunction results


I'm using Flash Builder 4, and I have an external XML file called words.xml with the following structure:

<wordList>
    <wordRecord>
        <word>duck</word>
        <syllables>1</syllables>
        <firstLetter>d</firstLetter>
        <!--other fields-->
    </wordRecord>
    <wordRecord>
        <word>machete</word>
        <syllables>3</syllables>
        <firstLetter>m</firstLetter>
        <!--other fields-->
    </wordRecord>
    <!--more wordRecords-->
</wordList>

Now, mind you, these aren't the exact field names or content, as those are proprietary to my client, whose XML file this is. But this is the basic structure. I need to display just the "word" field from each "wordRecord" in a List, which is going to be enabled with drag and drop and other goodies.

The relevant portions of the Flash Builder MXML file are:

<fx:Declarations>
    <fx:XML id="wordsXML" source="/xml/words.xml" />
<fx:Declarations>
<s:List id="resultsList">
    <s:dataProvider>
        <s:XMLListCollection id="xmlWords" source="{wordsXML..word}" />
    </s:dataProvider>
</s:List>

So far, so good. The List displays just fine in my Air application, scrolls and everything, and when the proper attributes are present, drag and drop works great.

What I need however, is to be able to filter the list according to the other fields, and display just the words from the result.

For present purposes, let's suppose that the XML file contains many, many "wordRecord" elements, and I need to create a filterFunction that ultimately will give me just the "word"s whose "firstLetter" is, let's assume "m".

I tried creating a filterFunction based on some examples I found around the web, but as my XMLListCollection is only the fields, I can't get the filterFunction to work with the other XML fields. I tried changing the XMLListDeclaration to give me each "wordRecord" element as an XMLList, but then I can't figure out how to get the s:List to display only "word"s from the results of the filterFunction.

Any ideas? Thanks in advance.


Solution

  • <s:List id="resultsList" labelField="word">
        <s:dataProvider>
            <s:XMLListCollection
                    id="xmlWords"
                    source="{wordsXML..wordRecord}"
                    />
        </s:dataProvider>
    </s:List> 
    

    This way you have the 'wordRecord' object for your filter function;