25
Sep
07

Creating a simple label function on a Flex ComboBox control

The following example shows how you can format the label in a ComboBox using a custom label function.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/25/creating-a-simple-label-function-on-a-flex-combobox-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.utils.StringUtil;

            private function comboBox_labelFunc(item:Object):String {
                return StringUtil.substitute("{0} ({1})", item.@name, item.@abbrev);
            }
        ]]>
    </mx:Script>

    <mx:XMLList id="statesXMLList">
        <state abbrev="AL" name="Alabama" />
        <state abbrev="AK" name="Alaska" />
        <state abbrev="AZ" name="Arizona" />
        <state abbrev="AR" name="Arkansas" />
        <state abbrev="CA" name="California" />
        <state abbrev="CO" name="Colorado" />
        <state abbrev="CT" name="Connecticut" />
    </mx:XMLList>

    <mx:ComboBox id="comboBox"
            prompt="Please select a State..."
            dataProvider="{statesXMLList}"
            labelFunction="comboBox_labelFunc" />

</mx:Application>

View source is enabled in the following example.


11 Responses to “Creating a simple label function on a Flex ComboBox control”


  1. 1 coco Sep 25th, 2007 at 10:58 pm

    well,I am a new learner in flex programming.yesterday,I found this website,and then I fell in interesting here.There are so many flexexamples that I read them almost in my work time. Thanks to your examples ,I acquire much knowledge.
    But I do not well in flex coding.I donot know sytax of flex wery well.How do I do now?Are there some skills in learning flex?I really love flex programming!
    Please tell me some ways to learn flex.Thank you!

  2. 2 peterd Sep 25th, 2007 at 11:28 pm

    coco,

    If you want to learn Flex, I highly recommend picking up a book or two and starting there. There is an excellent list of Flex/ActionScript books listed over on Flex.org at http://flex.org/books/.

    Also, there is an excellent list of resources (such as user groups, forums, mailing lists) over at http://flex.org/community/.

    Finally, you can find a lot of excellent articles over at the Adobe Flex Developer Center. Plus you can find a lot of other articles that people blog about by checking out MXNA, at http://weblogs.macromedia.com/mxna/.

    (And of course, you can always subscribe to this blog’s RSS feed at http://feeds.feedburner.com/FlexExamples.)

    Happy Flexing!

    Peter

  3. 3 coco Sep 25th, 2007 at 11:53 pm

    peter,
    Thanks for your warm answer!

    I am a chinese and now living in GuangZhou.So I think I can not buy the books from http://flex.org/books/.My english is not very well!When I read the article,I can not understand it entirely.

    I have just graduated from college,.Two week ago,I got a job from this company,and the company arranged flex project to me.At that time,I heard the word “flex” for the first time!In my company,I do the job of flex alone!I cannot ask anybody when I had a question.It makes me learn hard.

    Now the process goes very slow,I have a lot of things about flex to learn!How can I happy Flexing!

    coco

  4. 4 JD Sep 26th, 2007 at 12:30 am

    I just LOVE your site and the time that you put into sharing this info. Thank you.

  5. 5 peterd Sep 26th, 2007 at 7:05 am

    coco,

    I did a quick search, but didn’t see any Flex books in Chinese. You could try contacting Adobe Press (www.adobepress.com) and see if they have their Adobe Flex Classroom in a Book translated into Chinese, or if they plan to in the future.

    But I did find this Chinese Flex XML News Aggregator: http://www.rxna.cn/.

    There also seems to be a Chinese FlexCoders list at http://www.flexcoders.cn/.

    Hope that helps,

    Peter

  6. 6 coco Sep 26th, 2007 at 6:07 pm

    peter,
    Yes,it’s great help!I will learn more and more flexcoding from your blog and anywhere!

    I like flexing!Thank you for your help!

    coco

  7. 7 dormouse Sep 27th, 2007 at 8:39 pm

    coco

    You can find Flex books in Chinese in this webSite www.zhuoqun.net.
    Hope to help you.

    dormouse

  8. 8 Jimmy Oct 4th, 2007 at 2:47 am

    Hi peter,

    Just to let you know, you have alot of ‘loyal’ readers everywhere around the globe.
    I’m from Kuala Lumpur, Malaysia.

    happy flex-ing!

  9. 9 Anonymous Oct 18th, 2007 at 9:54 pm

    Lovely conepts that you give us……thankyou

  10. 10 Business Minded May 25th, 2008 at 8:20 am

    Thanks a lot Peter. I’m from India and you are indeed providing us all loads of value.

  11. 11 Blair Jul 29th, 2008 at 10:29 am

    Hi, fantastic site! I’m new at Flex and I’m having trouble with the above. Works great, except I can’t figure out how to something with what appears in the combobox? While the label appears to be formatted, showing the correct XML rows, the actually stored data variable still shows [object:Object]. Any help on where I can figure this out? Thanks.

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;".