Setting a label function on a FxList control in Flex Gumbo

by Peter deHaan on March 8, 2009

in FxList, beta

In a previous example, “Setting a label field on a FxList control in Flex Gumbo”, we saw how you could set the label field on the Flex Gumbo FxList control by setting the labelField property.

The following example shows how you can set a custom label function on a Flex Gumbo FxList control by setting the labelFunction property.

Support for the labelField and labelFunction properties on the FxList control was added in trunk build 5181. To download newer builds of the Flex Gumbo SDK, visit the “Gumbo Downloads” page on opensource.adobe.com

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2009/03/08/setting-a-label-function-on-a-fxlist-control-in-flex-gumbo/ -->
<FxApplication name="FxList_labelFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            private function list_labelFunc(item:Object):String {
                var nameStr:String = item.name;
                var priceStr:String = currFormatter.format(item.price);
                return nameStr + " -- " + priceStr;
            }
        ]]>
    </Script>

    <Declarations>
        <CurrencyFormatter id="currFormatter" precision="2" />
    </Declarations>

    <FxList id="list"
            labelFunction="list_labelFunc"
            horizontalCenter="0"
            verticalCenter="0">
        <dataProvider>
            <ArrayCollection>
                <Object name="The"   price="0.23" />
                <Object name="quick" price="1.03" />
                <Object name="brown" price="0.98" />
                <Object name="fox"   price="1.19" />
                <Object name="jumps" price="0.28" />
                <Object name="over"  price="0.42" />
                <Object name="the"   price="0.09" />
                <Object name="lazy"  price="0.81" />
                <Object name="dog"   price="0.72" />
            </ArrayCollection>
        </dataProvider>
    </FxList>

</FxApplication>

Or, if you can use an XMLListCollection and the labelFunction property, as seen in the following example:

View MXML

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2009/03/08/setting-a-label-function-on-a-fxlist-control-in-flex-gumbo/ -->
<FxApplication name="FxList_labelFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            private function list_labelFunc(item:Object):String {
                return item.@name + " -- " + currFormatter.format(item.@price);
            }
        ]]>
    </Script>

    <Declarations>
        <CurrencyFormatter id="currFormatter" precision="2" />
    </Declarations>

    <FxList id="list"
            labelFunction="list_labelFunc"
            horizontalCenter="0"
            verticalCenter="0">
        <dataProvider>
            <XMLListCollection>
                <source>
                    <XMLList>
                        <node name="The"   price="0.23" />
                        <node name="quick" price="1.03" />
                        <node name="brown" price="0.98" />
                        <node name="fox"   price="1.19" />
                        <node name="jumps" price="0.28" />
                        <node name="over"  price="0.42" />
                        <node name="the"   price="0.09" />
                        <node name="lazy"  price="0.81" />
                        <node name="dog"   price="0.72" />
                    </XMLList>
                </source>
            </XMLListCollection>
        </dataProvider>
    </FxList>

</FxApplication>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2009/03/08/setting-a-label-function-on-a-fxlist-control-in-flex-gumbo/ -->
<FxApplication name="FxList_labelFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white"
        initialize="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.components.FxList;
            import mx.formatters.CurrencyFormatter;

            private var arrColl:ArrayCollection;
            private var currFormatter:CurrencyFormatter;
            private var list:FxList;

            private function init():void {
                currFormatter = new CurrencyFormatter();
                currFormatter.precision = 2;

                var arr:Array = [];
                arr.push({name:"The",   price:0.23});
                arr.push({name:"quick", price:1.03});
                arr.push({name:"brown", price:0.98});
                arr.push({name:"fox",   price:1.19});
                arr.push({name:"jumps", price:0.28});
                arr.push({name:"over",  price:0.42});
                arr.push({name:"the",   price:0.09});
                arr.push({name:"lazy",  price:0.81});
                arr.push({name:"dog",   price:0.72});

                arrColl = new ArrayCollection(arr);

                list = new FxList();
                list.labelFunction = list_labelFunc;
                list.dataProvider = arrColl
                list.horizontalCenter = 0;
                list.verticalCenter = 0;
                addElement(list);
            }

            private function list_labelFunc(item:Object):String {
                var nameStr:String = item.name;
                var priceStr:String = currFormatter.format(item.price);
                return nameStr + " -- " + priceStr;
            }
        ]]>
    </Script>

</FxApplication>

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: