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.

15 thoughts on “Specifying a custom label field on a Flex ComboBox control

  1. 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. 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. 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. 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. 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. 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?

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

  8. 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

  9. 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

  10. 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.

  11. Hi Mr Peterd,
    I am BinhNguyen,
    I have a problem about AutoComplete extends from Combobox control .
    I want set Text property of it = “” when click reset button.
    folow my code :

    // code hander reset
    public function reset_click(){
    AC.promt=””;
    AC.seletedIndex=-1;
    AC.text=””;
    }

    It not good!

    Please , can you help me ?
    Thanks, best regard!

Comments are closed.