29
Oct
07

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.


10 Responses to “Defining a custom label function on a Flex Tree control”


  1. 1 Orioles Magic Apr 11th, 2008 at 9:33 pm

    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. 2 Nolan Jul 1st, 2008 at 1:40 pm

    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. 3 Manuel Sep 10th, 2008 at 3:10 am

    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. 4 peterd Sep 10th, 2008 at 7:03 am

    Manuel,

    I’m not sure I’ve seen that, but if you think it is a bug, can you please file a bug at http://bugs.adobe.com/flex/ and include some sample code and somebody can investigate it.

    Thanks,
    Peter

  5. 5 Manuel Sep 10th, 2008 at 12:40 pm

    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!

  6. 6 Manuel Sep 10th, 2008 at 1:27 pm

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

  7. 7 Manuel Sep 10th, 2008 at 1:32 pm

    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.

  8. 8 peterd Sep 10th, 2008 at 1:45 pm

    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

  9. 9 Manuel Sep 11th, 2008 at 4:04 am

    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!

  10. 10 abdulrahim Oct 6th, 2008 at 5:58 am

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

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