Specifying a custom label field on a Flex ComboBox control

by Peter deHaan on September 25, 2007

in ComboBox, XMLList

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.

{ 13 comments… read them below or add one }

1 dormouse September 25, 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

Reply

2 peterd September 25, 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

Reply

3 Greg October 2, 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

Reply

4 peterd October 2, 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

Reply

5 Greg October 3, 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

Reply

6 peterd October 3, 2007 at 9:41 pm

Greg,

You mean like this?:

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

Peter

Reply

7 Doug November 3, 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?

Reply

8 peterd November 4, 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

Reply

9 smarkamyth March 5, 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

Reply

10 peterd March 5, 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

Reply

11 Steve June 25, 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.

Reply

12 Steve June 25, 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

Reply

13 Denis Beurive January 8, 2009 at 1:17 pm

Merci encore pour ce super site!

Reply

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: