Flex Examples
Using an Array as a data provider in a Flex Tree control
The following code shows you how you can use an Array as a data provider for a Tree control in Flex.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/12/03/using-an-array-as-a-data-provider-in-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:Object):String {
var suffix:String = "";
if (tree.dataDescriptor.hasChildren(item)) {
suffix = " (" + item.children.length + ")";
}
return item.name.toUpperCase() + suffix;
}
]]>
</mx:Script>
<mx:Array id="arr">
<mx:Object name="One">
<mx:children>
<mx:Array>
<mx:Object name="Two" />
<mx:Object name="Three" />
<mx:Object name="Four">
<mx:children>
<mx:Array>
<mx:Object name="Five" />
<mx:Object name="Six" />
</mx:Array>
</mx:children>
</mx:Object>
<mx:Object name="Seven">
<mx:children>
<mx:Array>
<mx:Object name="Eight" />
</mx:Array>
</mx:children>
</mx:Object>
<mx:Object name="Nine" />
</mx:Array>
</mx:children>
</mx:Object>
</mx:Array>
<mx:Tree id="tree"
dataProvider="{arr}"
labelFunction="tree_labelFunc"
width="200" />
</mx:Application>
View source is enabled in the following example.
And if you happen to be using Flex 4 (download), the following example should work:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<fx:Script>
<![CDATA[
private function tree_labelFunc(item:Object):String {
var suffix:String = "";
if (tree.dataDescriptor.hasChildren(item)) {
suffix = " (" + item.children.length + ")";
}
return item.name.toUpperCase() + suffix;
}
]]>
</fx:Script>
<fx:Declarations>
<fx:Array id="arr">
<fx:Object name="One">
<fx:children>
<fx:Array>
<fx:Object name="Two" />
<fx:Object name="Three" />
<fx:Object name="Four">
<fx:children>
<fx:Array>
<fx:Object name="Five" />
<fx:Object name="Six" />
</fx:Array>
</fx:children>
</fx:Object>
<fx:Object name="Seven">
<fx:children>
<fx:Array>
<fx:Object name="Eight" />
</fx:Array>
</fx:children>
</fx:Object>
<fx:Object name="Nine" />
</fx:Array>
</fx:children>
</fx:Object>
</fx:Array>
</fx:Declarations>
<mx:Tree id="tree"
dataProvider="{arr}"
labelFunction="tree_labelFunc"
width="200"
horizontalCenter="0"
verticalCenter="0" />
</s:Application>
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.
7 Responses to Using an Array as a data provider in 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


Nice one.
Do you know how I can navigatie in a viewstack with a tree?
public function treeChanged(event:Event):void { if(event.target.selectedItem.@vs == "") { Alert.show("empty"); } else { contentA.selectedChild = Tree(event.target).selectedItem.@vs as Container; }But that doesn’t work.
Just hardcoded stuff….. its nothing special
hi , iam kiran
can u please suggest me, the solution for the problem.
i had arraycollection with label and id, i want to provide it to the tree, but i want the list of label to come under the clicable folder,
iam only able to display it in normal flatlist of labels
hope for the solutions
thanks
kiran
Do you think we can get a sample where you use the “connect to XML” data services feature in Flex 4? I really want to start using this but I can’t seem to figure out how to get it to properly serialize the data so it makes sense for the tree control.
well, I tried using your code but it does not work, do the same as you, but instead of that I return the list of data, I return this: [Object Object] on all nodes, anyone know why??
@mattlink,
You probably need to specify a
labelFunctionwhich tells the Tree control which item from the object to use for the label.Peter
Why adobe doesn’t show this example in their help boggles my mind. Sooooo much easier and flexible than using XMLListCollection.