Flex Examples
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.
<?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>
<?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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
13 Responses to Defining a custom label function on a Flex Tree control
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


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.
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.
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!
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
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!
YEAH! I got it done by myself :) Here’s the code of the overridden DefaultDataDescriptor class. Works great :)
[snip /]
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.
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 < and your > characters to >, otherwise, “bad things happen”.
But I can delete the partial code block if you want…
Peter
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!
It’s very nice tutorial for seperating the node and subnode value in tree component using lable function
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
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
sorry they are coming out as & g e ;
without the spaces