Creating a custom icon function on a Flex Tree control

by Peter deHaan on November 15, 2007

in Tree

The following example shows how you can build a custom icon function on a Flex Tree control to display different icons depending on the current node name in the data provider.

Full code after the jump.

View MXML

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

    <mx:Script>
        <![CDATA[
            [Bindable]
            [Embed("assets/bullet_go.png")]
            private var myBulletGoIcon:Class;

            [Bindable]
            [Embed("assets/bullet_star.png")]
            private var myBulletStarIcon:Class;

            [Bindable]
            [Embed("assets/bullet_wrench.png")]
            private var myBulletWrenchIcon:Class;

            private function tree_iconFunc(item:Object):Class {
                var iconClass:Class;
                switch (XML(item).localName()) {
                    case "league":
                        iconClass = myBulletGoIcon;
                        break;
                    case "division":
                        iconClass = myBulletStarIcon;
                        break;
                    case "team":
                        iconClass = myBulletWrenchIcon;
                        break;
                }
                return iconClass;
            }
        ]]>
    </mx:Script>

    <mx:XML id="dp">
        <mlb>
            <league label="American League">
                <division label="East">
                    <team label="Boston" />
                    <team label="New York" />
                    <team label="Toronto" />
                    <team label="Baltimore" />
                    <team label="Tampa Bay" />
                </division>
                <division label="Central">
                    <team label="Cleveland" />
                    <team label="Detroit" />
                    <team label="Minnesota" />
                    <team label="Chicago" />
                    <team label="Kansas City" />
                </division>
                <division label="West">
                    <team label="Los Angeles" />
                    <team label="Seattle" />
                    <team label="Oakland" />
                    <team label="Texas" />
                </division>
            </league>
        </mlb>
    </mx:XML>

    <mx:Tree id="TreeProject"
            dataProvider="{dp.league}"
            labelField="@label"
            showRoot="true"
            iconFunction="tree_iconFunc"
            width="320"
            height="240" />

</mx:Application>

View source is enabled in the following example.

{ 7 comments… read them below or add one }

1 Anonymous December 23, 2007 at 10:15 am

Is there no way the switch case can be made more dynamic? Is there a way to dynamically create the icon class? Because the switch-case forces me to declare every icon class that I would potentially want to use

Reply

2 Martin February 21, 2008 at 7:56 am

Great,

Just what I was looking for.. Keep up the good work!! ps. maybee you can have a little lighter color on this blog, so it doesnt feel like a warez site..thanks again for the example..

Reply

3 Partha September 5, 2008 at 3:42 am

Hi,

The above example is nice one & I do have a same kind of requirement. But after implementing the above concept I’m finding another issue. I don’t want to display the default ARROW (“>”) icon before the icon images. But once I remove/shade it the tree node expansion does not work. Can you please advice how this can be done?

Many Thanks

Cheers

Partha

Reply

4 peterd September 5, 2008 at 8:10 am
5 Partha September 6, 2008 at 12:42 am

Hi Peter,

Thanks for your reply, but unfortunately the above example is not working for my case. My application is bit complex. Let me explain how my application is working:

1. Initially I have a static XML, where most are parent node (0′th level) and few of them has child node (1st level).
2. I call a function from “ItemOpen” property of the tree. This take “TreeEvent”. This function fetch data for 2nd level (if the parent level is 0 or 3rd level data if parent level is 1).
3. Again when, newly populated node (whether it is 2nd or 3rd level) is opened the function retrieve 4th level data…so on.

Now, in your example expansion & collasp is handle by “ListEvent” event type. But same thing I could not replicate in my application where we have “”TreeEvent” as event type.

Not sure, whether I am able to make you understand about the problem or not? But if you have any further more advice then please let me know. It will be great help for me.

Many Thanks

Partha

Reply

6 Luca September 11, 2008 at 2:49 am

Hi all.

Thank you for your examples.

But I can’t understand how to make it dinamic.

I find the icon path in a XML document, but I’m not able to attach it to a tree…

Reply

7 Nico February 17, 2009 at 9:37 am

Hi all.
I tried this feature but the “Item” value of the function SetIcons is always null.
For information i use a http service to “import” a XML File. So i get always as icon the default icon.

Someone has an idea ?

Thanks.

Nico

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: