+展开
-ActionScript
private var data:Object = [{name:"Vehicles", id:1, parentId:0, type:"parent"},
{name:"Automobiles", id:2, parentId:1, type:"parent"},
{name:"Boats", id:3, parentId:0, type:"parent"},
{name:"Trucks", id:4, parentId:1, type:"parent"},
{name:"Sedans", id:5, parentId:2, type:"parent"}];
+展开
-ActionScript
package oreilly.cookbook{
import flash.events.EventDispatcher;
import mx.collections.IHierarchicalData;
public class ObjectHierarchicalData extends EventDispatcher
implements IHierarchicalData
{
private var source:Object;
public function ObjectHierarchicalData(value:Object)
{
super();
source = value;
} /* in our simple system, only parents with their type set
to 'parent' can have children; otherwise, they can't have
children */
public function canHaveChildren(node:Object):Boolean
{
if (node.type == "parent") {
return true;
}
return false;
}
/* for any given node, determine whether a node has any
children by looking through all the other nodes for that node's
ID as a parentTask */
public function hasChildren(node:Object):Boolean
{
trace(node.name);
var children:Array = new Array();// = source.parentTask
== parentId);
for each(var obj in source) {
if(obj.parentTask == node.id) {
children.push(obj);
}
}
if (children.length > 0)
return true;
return false;
} /* for any given node, return all the nodes that are
children of that node in an array */
public function getChildren(node:Object):Object
{
var parentId:String = node.id;
var children:Array = new Array();
for each(var obj in source) {
if(obj.parentTask == parentId) {
children.push(obj);
}
}
return children;
}
public function getData(node:Object):Object
{
for each(var obj in source) {
for(var prop in node) {
if(obj[prop] == node[prop]) {
return obj;
} else {
break;
}
}
}
return null;
} /* we want to return every obj that is a
root object
which, in this case, is going to be all nodes that have a
parent node of '0' */
public function getRoot():Object
{
var rootsArr:Array = new Array();
for each(var obj in source) {
if(obj.parentTask == "0") {
rootsArr.push(obj);
}
}
return rootsArr;
}
public function getParent(node:Object):*
{
for each(var obj in source) {
if(obj.parentTask == node.parentTask) {
return obj;
}
}
return null;
}
}
}
+展开
-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400"
height="300" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.collections.*;
[Bindable]
private var ohd:ObjectHierarchicalData;
/* here'sthehugeobjectthatwe're going to use to
populate our ObjectHierarchicalData object */
private var largeObject:Object =
[{
"id":"1", "name":"Misc", "type":"parent",
"parentTask":"0"},
{"id":"2", "name":"Clean the kitchen", "type":"parent",
"parentTask":"0"},
{"id":"3", "name":"Pay the bills", "type":"parent",
"parentTask":"0"},
{"id":"4", "name":"Paint the shed", "type":"parent",
"parentTask":"1"},
{"id":"5", "name":"Get ready for party",
"type":"parent",
"parentTask":"1"},
{"id":"6", "name":"Do the dishes", "type":"child",
"parentTask":"2"},
{"id":"7", "name":"Take out trash", "type":"child",
"parentTask":"2"},
{"id":"8", "name":"Gas Bill", "type":"child",
"parentTask":"3"},
{"id":"9", "name":"Registration", "type":"child",
"parentTask":"3"},
{"id":"10", "name":"Fix the car", "type":"parent",
"parentTask":"0"},
{"id":"11", "name":"New tires", "type":"child",
"parentTask":"10"},
{"id":"12", "name":"Emissions test", "type":"child",
"parentTask":"10"},
{"id":"13", "name":"Get new paint", "type":"child",
"parentTask":"4"},
{"id":"14", "name":"Buy brushes", "type":"child",
"parentTask":"4"},
{"id":"15", "name":"Buy Drinks", "type":"child",
"parentTask":"5"},
{"id":"16", "name":"clean living room", "type":"child",
"parentTask":"5"},
{"id":"16", "name":"finish invitations", "type":"child",
"parentTask":"5"} ];
private function init():void {
ohd = new ObjectHierarchicalData(largeObject);
}
]]>
</mx:Script>
<mx:AdvancedDataGrid dataProvider="{ohd}" width="300"
height="200">
<mx:columns>
<!-- all we want to display of the object is the name,
the ADG will take care of displaying the parent child
relationship -->
<mx:AdvancedDataGridColumn dataField="name"/>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:Canvas>