15
Nov
07

Setting a custom icon field in a Flex Tree control

The following example shows how you can specify a custom icon field in a Flex Tree control to set an icon on a specific node.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/15/setting-a-custom-icon-field-in-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_star.png")]
            public var myBulletStarIcon:Class;
        ]]>
    </mx:Script>

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

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

</mx:Application>

View source is enabled in the following example.


6 Responses to “Setting a custom icon field in a Flex Tree control”


  1. 1 Gary Nov 15th, 2007 at 4:40 am

    Thanks. How about an example of dynamically setting the icon based on some data values?

  2. 2 Gary Nov 15th, 2007 at 4:42 am

    Never mind. I just needed to look at your other example. Thanks!

  3. 3 Anonymous Jan 24th, 2008 at 11:55 pm

    hi,How to hide the icon???
    I want only the Arrow display not Folder icon.

  4. 4 peterd Jan 25th, 2008 at 12:05 am

    Anonymous,

    Try something like the following:

    <mx:Style>
        Tree {
            folder-closed-icon: ClassReference(null);
            folder-open-icon: ClassReference(null);
        }
    </mx:Style>
    

    Hope that helps,
    Peter

    UPDATE: In fact, I just posted a new entry, “Removing folder icons from the Flex Tree control”, if you want a full example.

  5. 5 Avery Apr 8th, 2008 at 9:07 am

    Thank you for the code.

  6. 6 Rick Apr 21st, 2008 at 10:38 am

    Any chance you’ve got an example that allows for setting a unique icon based on the level of nesting? I.e., I’d like to have one icon for the first level of hierarchy, a different one for the second, etc.

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