Specifying a custom icon function on a ComboBox control in Flex

by Peter deHaan on July 13, 2008

in ComboBox

The following example shows how you can specify a custom icon function on a Flex ComboBox control by setting the iconFunction property on the ComboBox control’s dropdown menu.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/13/specifying-a-custom-icon-function-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.DropdownEvent;

            [Embed("assets/status_online.png")]
            private var statusOnline:Class;

            private function comboBox_open(evt:DropdownEvent):void {
                comboBox.dropdown.iconFunction = comboBoxDropdown_func;
            }

            private function comboBoxDropdown_func(item:Object):Class {
                if (item.hasOwnProperty("online") && item.online) {
                    return statusOnline;
                }
                return null;
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="One" online="true" />
        <mx:Object label="Two" />
        <mx:Object label="Three" online="true" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" online="true" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}"
            open="comboBox_open(event);" />

</mx:Application>

View source is enabled in the following example.

{ 5 comments… read them below or add one }

1 Klaus Busse July 14, 2008 at 12:28 am

Since a few days the examples are just shown as code, not as a working example. I see this as a step back. While the loading time of the page increased of course, it takes longer to see what’s going on, and to see that, every reader has to build it. IMO this reduces the impact of this blog, because seeing is believing.

Thanks anyway for your great work here!

Klaus

Reply

2 Romz July 14, 2008 at 1:54 am

I agree with Klaus !!!

Reply

3 junior.flex July 14, 2008 at 3:49 am

it’s rigth, i’m french and every days,
i see the blog because it show greats flex codes exemples.

The demonstrations are very important !!!

Reply

4 peterd July 14, 2008 at 8:40 am

Yeah, I agree with Klaus too, they *should* have examples. If I get time over the next few days I’ll go back and upload SWF examples. But even without SWFs, it is pretty easy to copy/paste into Flex Builder and see how the code works (this example is marginally trickier since you have to find a PNG — or you can grab the PNG file from http://www.famfamfam.com/lab/icons/silk/).

Peter

Reply

5 Klaus Busse July 14, 2008 at 9:32 am

Hi Peter, thanks for the feedback! It’s ok if they are missing from time to time – you certainly have also other tasks on your plate. Just wanted to make sure that this didn’t happen by intention… and to express that it really provides value.

Cheers

Klaus

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: