Defining a custom label function on a Flex Tree control

The following example shows how you can specify a custom label function on a Tree control in Flex where the returned label is specific to the node name.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/29/defining-a-custom-label-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[
            private function tree_labelFunc(item:XML):String {
                var label:String;
                switch (item.localName()) {
                    case "league":
                        label = item.@title;
                        break;
                    case "team":
                        label = item.@name;
                        break;
                    case "stadium":
                        label = item.name;
                }
                return label;
            }
        ]]>
    </mx:Script>

    <mx:XML id="mlb" source="mlb.xml" />

    <mx:Tree id="tree"
            dataProvider="{mlb.league}"
            labelFunction="tree_labelFunc"
            width="300"
            rowCount="8" />

</mx:Application>

View mlb.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/29/defining-a-custom-label-function-on-a-flex-tree-control/ -->
<mlb>
    <league id="al" title="American League">
        <team name="Baltimore Orioles" />
        <team name="Boston Red Sox" />
        <team name="Chicago White Sox" />
        <team name="Cleveland Indians" />
        <team name="Detroit Tigers" />
        <team name="Kansas City Royals" />
        <team name="Los Angeles Angels of Anaheim" />
        <team name="Minnesota Twins" />
        <team name="New York Yankees" />
        <team name="Oakland Athletics" />
        <team name="Seattle Mariners" />
        <team name="Tampa Bay Devil Rays" />
        <team name="Texas Rangers" />
        <team name="Toronto Blue Jays" />
    </league>
    <league id="nl" title="National League">
        <team name="Arizona Diamondbacks" />
        <team name="Atlanta Braves" />
        <team name="Chicago Cubs" />
        <team name="Cincinnati Reds" />
        <team name="Colorado Rockies" />
        <team name="Florida Marlins" />
        <team name="Houston Astros" />
        <team name="Los Angeles Dodgers" />
        <team name="Milwaukee Brewers" />
        <team name="New York Mets" />
        <team name="Philadelphia Phillies" />
        <team name="Pittsburgh Pirates" />
        <team name="San Diego Padres" />
        <team name="San Francisco Giants" />
        <team name="St. Louis Cardinals" />
        <team name="Washington Nationals" />
    </league>
</mlb>

View source is enabled in the following example.

13 thoughts on “Defining a custom label function on a Flex Tree control

  1. That’s a good example. You had me psyched out there for a minute. I found this page doing a search for the Orioles, but I do some programming on the side too so this is good to know.

  2. Thanks for all of the great examples.
    Is there a way to hide or display a customized Arrow Icon, instead of using the default gray arrow?

    thx.

  3. Hey, thank you for the neat introduction into custom node/leaf labeling of the Tree component. Btw. do you, or does anyone know how to hide CDATA sections within the tree? When I have such sections in the Tree dataprovider, those leafs will be displayed, but I don’t know how to hide ’em. When I click onto such a leaf and I continue navigation through the tree, arbitrary nodes get hidden and show up again. This issue drives me nuts!

  4. Yes, the randomly hidden node issue is certainly a bug and I will file it as soon I’ll get the time for that.
    Regarding the unwanted display of CDATA tags, I think it is more a missing feature than a bug. Can you perhaps tell me how I could circumvent that odd behavior, e.g. by subclassing the tree item renderer? I just want to exclude the CDATA sections from the tree display, while keeping them in the data provider and being able to access and change them.

    Thank you for any hints on how to achieve this!

  5. YEAH! I got it done by myself :) Here’s the code of the overridden DefaultDataDescriptor class. Works great :)
    [snip /]

  6. Noooo :( too much characters, please delete the code in the post…
    It’s just the following:
    I subclassed the DefaultDataDescriptor, overrode the isBranch() method, iterated through the childList and returned false if child node’s localName() was null (e.g. is a CDATA section). Period.

  7. Manuel,

    There shouldn’t be a character limit that I know of (certainly none that I’ve reached). If you want to post code blocks though, you need to convert your < characters to &lt; and your > characters to &gt;, otherwise, “bad things happen”.

    But I can delete the partial code block if you want…

    Peter

  8. It’s okay, I used the HTML entities as suggested, but something went wrong. I think my description of the workaround should be sufficient for most of you ;) Thanks for your efforts!

  9. It’s very nice tutorial for seperating the node and subnode value in tree component using lable function

  10. I need a flex tree control where it have lot of nodes containing html page link, when we select the node respected html page should be loaded in other container in the same window, can u provide one example, that will be helpful………

    regards,
    Naresh

  11. Hello,

    Great site!! So here is my question. I have a Tree with a custom TreeRenderer. I have XML as the dataProvider and the labelField has html in it. I am able to display the text with the html formatting with one exception. The less than and greater than symbols come out as ≥

    Here is what the XML attribute is
    Desc=”Asthma, ≥ One:

    This does come out as (bold as desired)
    Asthma, ≥ One:

    I tried creating a function to wrap the value in CDATA but seems not to display the >

    Thanks
    RC Army

Comments are closed.