Flex - 树控件



介绍

树控件显示以可扩展树的形式排列的分层数据。

类声明

以下是mx.controls.Tree类的声明:

public class Tree 
   extends List
      implements IIMESupport

公共属性

序号 属性及描述
1

dataDescriptor : mx.controls.treeClasses:ITreeDataDescriptor

返回当前的 ITreeDataDescriptor。

2

dataProvider : Object

[覆盖] 包含要显示的数据的对象。

3

dragMoveEnabled : Boolean

[覆盖] 指示项目可以移动,而不仅仅是从树控件中复制作为拖放操作的一部分。

4

firstVisibleItem : Object

当前显示在树顶部行的项目。

5

hasRoot : Boolean

[只读] 指示当前 dataProvider 具有根项目;例如,分层结构中的单个顶部节点。

6

itemIcons : Object

指定项目图标的对象。

7

maxHorizontalScrollPosition : Number

[覆盖] 树控件的 maxHorizontalScrollPosition 属性的最大值。

8

openItems : Object

已打开或设置为打开的项目。

9

showRoot : Boolean

设置根项目的可见性。

公共方法

序号 方法及描述
1

Tree()

构造函数。

2

expandChildrenOf(item:Object, open:Boolean):void

打开或关闭指定项目下方的所有树项目。

3

expandItem(item:Object, open:Boolean, animate:Boolean = false, dispatchEvent:Boolean = false, cause:Event = null):void

打开或关闭分支项目。

4

getParentItem(item:Object):*

返回子项目的已知父级。

5

isItemOpen(item:Object):Boolean

如果指定项目分支已打开(显示其子项),则返回 true。

6

setItemIcon(item:Object, iconID:Class, iconID2:Class):void

设置与项目关联的图标。

受保护的方法

序号 方法及描述
1

dragCompleteHandler(event:DragEvent):void

[覆盖] 处理 DragEvent.DRAG_COMPLETE 事件。

2

dragDropHandler(event:DragEvent):void

[覆盖] 处理 DragEvent.DRAG_DROP 事件。

3

initListData(item:Object, treeListData:mx.controls.treeClasses:TreeListData):void

初始化树项目渲染器使用的 TreeListData 对象。

4

makeListData(data:Object, uid:String, rowNum:int):BaseListData

[覆盖] 创建一个新的 TreeListData 实例,并根据输入数据提供程序项目填充字段。

事件

序号 事件及描述
1

itemClose

分支关闭或折叠时调度。

2

itemOpen

分支打开或展开时调度。

3

itemOpening

初始化分支打开或关闭时调度。

继承的方法

此类继承自以下类的方法:

  • mx.controls.List
  • mx.controls.listClasess.ListBase
  • mx.core.ScrollControlBase
  • mx.core.UIComponent
  • mx.core.FlexSprite
  • flash.display.Sprite
  • flash.display.DisplayObjectContainer
  • flash.display.InteractiveObject
  • flash.display.DisplayObject
  • flash.events.EventDispatcher
  • Object

Flex 树控件示例

让我们按照以下步骤,通过创建一个测试应用程序来检查 Flex 应用程序中树控件的使用:

步骤 描述
1 在包 com.tutorialspoint.client 下创建一个名为 HelloWorld 的项目,如 Flex - 创建应用程序 章节中所述。
2 修改 HelloWorld.mxml,如下所述。保持其余文件不变。
3 编译并运行应用程序,以确保业务逻辑按要求工作。

以下是修改后的 mxml 文件 src/com.tutorialspoint/HelloWorld.mxml 的内容。

<?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/mx
   width = "100%" height = "100%" minWidth = "500" minHeight = "500">
   
   <fx:Style source = "/com/tutorialspoint/client/Style.css" />	
   <fx:Script>
      <![CDATA[
         [Bindable]
         public var selectedNode:XML;

         // Event handler for the Tree control change event.
         public function treeChanged(event:Event):void {
            selectedNode = Tree(event.target).selectedItem as XML;
         }
      ]]>
   </fx:Script>
   
   <fx:Declarations>
      <fx:XMLList id = "treeData">
         <node label = "E-Mail Box">
            <node label = "Inbox">
            <node label = "Client" />
               <node label = "Product" />
               <node label = "Personal" />
            </node>
            
            <node label = "Sent Items">
               <node label = "Professional" />
               <node label = "Personal" />
            </node>
            
            <node label = "Deleted Items" />
            <node label = "Spam" />
         </node>    
      </fx:XMLList>
   </fx:Declarations>
   
   <s:BorderContainer width = "630" height = "480" id = "mainContainer" 
      styleName = "container">
      <s:VGroup width = "100%" height = "100%" gap = "50" 
         horizontalAlign = "center" verticalAlign = "middle">
         <s:Label id = "lblHeader" text = "Complex Controls Demonstration" 
            fontSize = "40" color = "0x777777" styleName = "heading" />
            
         <s:Panel id = "treePanel" title = "Using Tree" 
            width = "500" height = "300">
            <s:layout>
               <s:VerticalLayout  gap = "10" verticalAlign = "middle" 
                  horizontalAlign = "center" />	
            </s:layout>
               
            <mx:Tree id = "tree" width = "95%" height = "75%" 
               labelField = "@label" showRoot = "false" 
               dataProvider = "{treeData}" change = "treeChanged(event)" />
            <s:TextArea height = "20%" width = "95%" 
               text = "Selected Item: {selectedNode.@label}" />
         </s:Panel>		
      </s:VGroup>	 
   </s:BorderContainer>	
</s:Application>

完成所有更改后,让我们像在Flex - 创建应用程序章节中所做的那样,以正常模式编译并运行应用程序。如果应用程序一切正常,它将产生以下结果:[ 在线尝试 ]

Flex Tree Control
flex_complex_controls.htm
广告

© . All rights reserved.