+展开
-ActionScript
package oreilly.cookbook {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
public class ComplexDataType extends EventDispatcher {
public static const NAME_CHANGED:String = "nameChanged" ;
public static const OFFICE_CHANGED:String = "officeChanged" ;
public static const RECORD_CHANGED:String = "recordChanged" ;
private var _name:Name;
private var _office:Office;
private var _label:String;
public function ComplexDataType(target:IEventDispatcher=null ) {
super(target);
}
[Bindable(RECORD_CHANGED)]
public function set label(value:String):void {
_label = value;
dispatchEvent(newEvent(RECORD_CHANGED));
}
public function get label():String { return _label; }
[Bindable(NAME_CHANGED)]
public function set name(value:Name):void {
_name = value;
dispatchEvent(newEvent(NAME_CHANGED));
}
public function get name():Name { return _name; }
[Bindable(OFFICE_CHANGED)]
public function set office(value:Office):void {
_office = value;
dispatchEvent(newEvent(OFFICE_CHANGED));
}
public function get office():Office { return _office; }
}
}
+展开
-ActionScript
package oreilly.cookbook {
import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;
import mx.controls.treeClasses.ITreeDataDescriptor;
public class ComplexDataCollection implements ITreeDataDescriptor {
public function getChildren(node:Object,model:Object=null):ICollectionView{
var col:ArrayCollection;
if(node is Office){
col = newArrayCollection([node.officeAddress,node.officeName]);
return col;
}
if (node is Name){
col = new ArrayCollection([node.firstName, node.lastName]);
return col;
}
if (node is ComplexDataType){
col = new ArrayCollection([node.office, node.name]);
return col;
}
return null ;
}
public function hasChildren(node:Object, model:Object=null ):Boolean {
if (node is Office){
if (node.officeAddress != "" && node.officeName != "" ){
return true ;
} else {
return false ;
}
}
if (node is Name){
if (node.firstName != "" && node.firstName != "" ){
return true ;
} else { return false ; }
}
if (node is ComplexDataType){
if (node.office != null && node.name != null ) {
return true ;
} else { return false ; }
}
return false ;
}
public function isBranch(node:Object, model:Object=null ):Boolean {
if (node is Office){
if (node.officeAddress != "" && node.officeName != "" ){
return true ;
} else {
return false ;
}
}
if (node is Name) {
if (node.firstName != "" && node.firstName != "" ){
return true ;
} else {
return false ;
}
}
if (node is ComplexDataType) {
if (node.office != null && node.name != null ) {
return true ;
} else { return false ;
}
}
return true ;
}
public function getData(node:Object, model:Object=null ):Object {
if (node is Office) {
return {children:{label:node.officeName, label:node.officeAddress}};
}
if (node is Name) {
return {children:{label:node.firstName, label:node.lastName}};
}
if (node is ComplexDataType){
return {children:{label:node.office, label:node.name}};
}
return null ;
}
public function addChildAt(parent:Object, newChild:Object, index:int,model:Object=null):Boolean {
return false ;}
public function removeChildAt(parent:Object, child:Object, index:int,model:Object=null):Boolean {
return false ;
}
}
}
+展开
-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" creationComplete="initCmp()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var dataProv:ArrayCollection;
private function initCmp():void {
var compData:ComplexDataType = new ComplexDataType();
compData.label = "13";
var _name:Name = new Name();
_name.firstName = "Joe" ;
_name.lastName = "Smith" ;
compData.name = _name;
var office:Office = new Office();
office.officeAddress = "544 Happy St. Anytown NY 01092" ;
office.officeName = "Happy Town Branch" ;
compData.office = office;
var compData2:ComplexDataType = new ComplexDataType();
compData2.label = "328" ;
var _name2:Name = new Name();
_name2.firstName = "Jane" ;
_name2.lastName = "Doe" ;
compData2.name = _name2;
var office2:Office = new Office();
office2.officeAddress = "544 Happy St. Anytown NY 01092" ;
office2.officeName = "Happy Town Branch" ;
compData2.office = office2;
dataProv = new ArrayCollection([compData, compData2]);
}
]]>
</mx:Script>
<mx:Tree dataDescriptor="{new ComplexDataCollection()}"
dataProvider="{dataProv}" labelField="label" width="300"/>
</mx:Canvas>