25
Sep
07

Specifying a custom label field on a Flex ComboBox control

The following example shows how you can specify a custom label field for a ComboBox control by using the ComboBox class’s labelField property.

The labelField property controls which field in the data provider will be used as the ComboBox item’s label. By default, the labelField property is “label”, although if you are using an XML data provider, you may want to change the labelField property to “@label”, or some other value.

Full code after the jump.

View MXML

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

    <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}"
            labelField="@name" />

</mx:Application>

View source is enabled in the following example.


12 Responses to “Specifying a custom label field on a Flex ComboBox control”


  1. 1 dormouse Sep 25th, 2007 at 8:34 pm

    Hi, peterd
    i am so sorry here to trouble you, but i have no idea to solve the problem.So i just to come here to get a help from you,how to play .wmv in the flex. In flex, it can play .swf and .flv, but can not play .wmv, i search it in the internet, the solution is convert .wmv to .flv. But it is so boring, do you have another method?
    Sorry again…

    dormouse

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

    dormouse,

    Sorry, currently the Flash Player does not support WMV files. I’m afraid you’ll have to convert to either SWF or FLV files, like you mentioned.

    Peter

  3. 3 Greg Oct 2nd, 2007 at 5:34 am

    Hey, nice website!
    I was woundering how to make the selected value different from the label value. In the example above how would I obtain the value of abbrev?

    You’ve been a great help!
    Thanks,

    Greg

  4. 4 peterd Oct 2nd, 2007 at 11:34 am

    Greg,

    To display the “abbrev” instead of the “name”, you could use the following code:

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

    Peter

  5. 5 Greg Oct 3rd, 2007 at 8:42 am

    Hey thanks. But I’d like to continue to use the same label, just access the data of another attribute. So somewhere in my application perform a query on the value of abbrev.
    Say I have a list of employees and employee numbers:

    etc..

    How would I display name in the combo box but access empnum in the code?

    Thanks for your help!

    Greg

  6. 6 peterd Oct 3rd, 2007 at 9:41 pm

    Greg,

    You mean like this?:

    var selectedAbbrev:String = String(comboBox.selectedItem.abbrev);
    

    Peter

  7. 7 Doug Nov 3rd, 2007 at 8:18 pm

    I posted this in the Adobe flex builder 3 forum, but I’ll ask you as well:

    I can’t get the combobox to display data properly. The server returns a list of photos and other data in xml. I want to have the combobox display a list of the photo id numbers, and let the user select a photo. When I use the xml as the dataprovider, then set LabelField to @id, it just says [object Object] in each of the items. When I use an item renderer inside the combobox (as shown in the attached code), the items display properly in the drop down part, but the Prompt is replaced with [object Object] and when an item is selected it shows as [object Object].

    Any ideas on how to fix this?

  8. 8 peterd Nov 4th, 2007 at 12:01 am

    Doug,

    I replied in the forum. Would you mind posting a sample of the XML in the Adobe forums (my blog comments are pretty hard to paste code into) and I can try and take a look.

    Peter

  9. 9 smarkamyth Mar 5th, 2008 at 7:44 pm

    Is it possible to script the encoding of wmv to flv when a user uploads the wmv file?

    example:
    User uploads WMV through Flex2/AS3 interface
    System auto-magically converts to FLV
    System stores FLV
    Stores a link to it for later display in Flex2/AS3 interface

    cheers,
    Smark A Myth

  10. 10 peterd Mar 5th, 2008 at 8:03 pm

    smarkamyth,

    I’ve never tried it, but I’m sure it is possible. You’d need a way to convert videos using a command line, I imagine.

    Something like:
    1) User uploads a WMV/source video file using FileReference (or HTML or whatever).
    2) Server-side script (ColdFusion, PHP, ASP.NET, Perl, Ruby, Java, etc) calls a command-line script to convert the video file from formatX to formatY (or even copies/FTPs the video file to a separate server for transcoding).
    3) Once transcoding is complete, delete the original WMV/source video file and store the FLV back on the web server (or wherever).

    I imagine the tricky part is finding the appropriate conversion software that works best for you.
    You may have better luck asking on a high-traffic list, such as FlexCoders.

    Peter

  11. 11 Steve Jun 25th, 2008 at 3:50 am

    Sorry for posting to an old thread, In your original example, how would I get the labelField of the combobox to display “(@abbrev) @name”. e.g. “(CA) California”.

    I’ve tried some labelFunctions, but though they work well with datagrids, I can’t seem to find out how to make them work with ComboBoxes.

    Your help would be greately appreciated.

    Cheers

    Steve.

  12. 12 Steve Jun 25th, 2008 at 3:54 am

    Sorry, didn’t read your other post on ComboBox labelFunctions :}

    http://blog.flexexamples.com/2007/09/25/creating-a-simple-label-function-on-a-flex-combobox-control/

    Thanks

    Steve

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