public function walkTree(tree:Tree, item:Object, startAtParent:Boolean = false):void
{
// get the Tree's data descriptor
var descriptor:ITreeDataDescriptor = tree.dataDescriptor;
var cursor:IViewCursor;
var parentItem:Object;
var childItem:Object;
var childItems:Object;
// if the item is null, stop
if(item == null)
return;
// do we back up one level to the item's parent
if(startAtParent)
{
// get the parent
parentItem = tree.getParentItem(item);
// is the parent real
if(parentItem)
{
trace("|-- Parent Node ", parentItem[tree.labelField]);
// if the parent is a branch
if(descriptor.isBranch(parentItem))
{
// if the branch has children to run through
if(descriptor.hasChildren(parentItem))
{
// get the children of the branch
// this part of the algorithm contains the item
// passed
childItems = descriptor.getChildren(parentItem);
}
}
// if the branch has valid child items
if(childItems)
{
// create our back step cursor
cursor = childItems.createCursor();
// loop through the items parent's children (item)
while(!cursor.afterLast)
{
// get the current child item
childItem = cursor.current;
var label:String = childItem[tree.labelField];
var branch:Boolean = descriptor.isBranch(childItem);
// good place for a custom method()
trace("Sibling Nodes :: ", label, "Is Branch :: ", branch);
// if the child item is a branch
if(descriptor.isBranch(childItem))
// traverse the childs branch all the way down
// before returning
walkTree(tree, childItem);
// do it again!
cursor.moveNext();
}
}
}
}
else// we don't want the parent OR this is the second iteration
{
// if we are a branch
if(descriptor.isBranch(item))
{
// if the branch has children to run through
if(descriptor.hasChildren(item))
{
// get the children of the branch
childItems = descriptor.getChildren(item);
}
// if the child items exist
if(childItems)
{
// create our cursor pointer
cursor = childItems.createCursor();
// loop through all of the children
// if one of these children are a branch we will recurse
while(!cursor.afterLast)
{
// get the current child item
childItem = cursor.current;
var label:String = childItem[tree.labelField];
var branch:Boolean = descriptor.isBranch(childItem);
// good place for a custom method()
trace("-- Sub Node :: ", label, "Is Branch :: ", branch);
// if the child item is a branch
if(descriptor.isBranch(childItem))
// traverse the childs branch all the way down
// before returning
walkTree(tree, childItem);
// check the next child
cursor.moveNext();
}
}
}
}
}
2:使用
假设有个树,随便写一个:
<tree name="test">
<node name="a">
<d name="b"> </d>
<e name="c"> </e>
</node>
<node name="1"> </node>
<node name="I"> </node>
</tree>;
选择起始节点为name="a"的那个节点
用walkTree(tree对象, 起始节点)则
直接走下半部,不回溯期父节点,直接遍历a下子节点,结果就是遍历b,c
用walkTree(tree对象, 起始节点,true)则
先回溯到a的父,即test再遍历,结果就是遍历test,a,b,c,1,I
{
// get the Tree's data descriptor
var descriptor:ITreeDataDescriptor = tree.dataDescriptor;
var cursor:IViewCursor;
var parentItem:Object;
var childItem:Object;
var childItems:Object;
// if the item is null, stop
if(item == null)
return;
// do we back up one level to the item's parent
if(startAtParent)
{
// get the parent
parentItem = tree.getParentItem(item);
// is the parent real
if(parentItem)
{
trace("|-- Parent Node ", parentItem[tree.labelField]);
// if the parent is a branch
if(descriptor.isBranch(parentItem))
{
// if the branch has children to run through
if(descriptor.hasChildren(parentItem))
{
// get the children of the branch
// this part of the algorithm contains the item
// passed
childItems = descriptor.getChildren(parentItem);
}
}
// if the branch has valid child items
if(childItems)
{
// create our back step cursor
cursor = childItems.createCursor();
// loop through the items parent's children (item)
while(!cursor.afterLast)
{
// get the current child item
childItem = cursor.current;
var label:String = childItem[tree.labelField];
var branch:Boolean = descriptor.isBranch(childItem);
// good place for a custom method()
trace("Sibling Nodes :: ", label, "Is Branch :: ", branch);
// if the child item is a branch
if(descriptor.isBranch(childItem))
// traverse the childs branch all the way down
// before returning
walkTree(tree, childItem);
// do it again!
cursor.moveNext();
}
}
}
}
else// we don't want the parent OR this is the second iteration
{
// if we are a branch
if(descriptor.isBranch(item))
{
// if the branch has children to run through
if(descriptor.hasChildren(item))
{
// get the children of the branch
childItems = descriptor.getChildren(item);
}
// if the child items exist
if(childItems)
{
// create our cursor pointer
cursor = childItems.createCursor();
// loop through all of the children
// if one of these children are a branch we will recurse
while(!cursor.afterLast)
{
// get the current child item
childItem = cursor.current;
var label:String = childItem[tree.labelField];
var branch:Boolean = descriptor.isBranch(childItem);
// good place for a custom method()
trace("-- Sub Node :: ", label, "Is Branch :: ", branch);
// if the child item is a branch
if(descriptor.isBranch(childItem))
// traverse the childs branch all the way down
// before returning
walkTree(tree, childItem);
// check the next child
cursor.moveNext();
}
}
}
}
}
2:使用
假设有个树,随便写一个:
<tree name="test">
<node name="a">
<d name="b"> </d>
<e name="c"> </e>
</node>
<node name="1"> </node>
<node name="I"> </node>
</tree>;
选择起始节点为name="a"的那个节点
用walkTree(tree对象, 起始节点)则
直接走下半部,不回溯期父节点,直接遍历a下子节点,结果就是遍历b,c
用walkTree(tree对象, 起始节点,true)则
先回溯到a的父,即test再遍历,结果就是遍历test,a,b,c,1,I