一个简单的递归调用

下面是一个递归调用,注意 arguments.callee()方法
<?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" minWidth="955" minHeight="600">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.events.TreeEvent;
			private function handleItemClose(evt:TreeEvent):void 
			{
				tree1.expandChildrenOf(evt.item, false);
			}
			
			private function init():void {
				this.tree1.addEventListener(TreeEvent.ITEM_CLOSE, handleItemClose);
			}
			
			private function getAllChildLabel():void 
			{
				if(tree1.selectedItem) {
					var allChild:Array = [];
					var xml:XML = tree1.selectedItem as XML;
					this.getAllChild(allChild, xml);
					this.textArea1.text = allChild.join("\n");
				}
			}
			private function getAllChild(allChild:Array, xml:XML):void 
			{
				allChild.push(xml.@label);
				if(xml.children().length()>0) {
					for each(var node:XML in xml.children())
					arguments.callee(allChild,node);
				}
			}
			
			private function getAllParentLabel():void
			{
				if(tree1.selectedItem) {
					var allParent:Array = [];
					var xml:XML = tree1.selectedItem as XML;
					this.getAllParent(allParent, xml);
					this.textArea1.text = allParent.join("\n");
				}
			}
			
			private function getAllParent(allParent:Array, xml:XML):void 
			{
				allParent.push(xml.@label);
				if(xml.parent()) {
					arguments.callee(allParent, xml.parent());
				}
			}
			
		]]>
	</fx:Script>
	<mx:Tree id="tree1" labelField="@label" showRoot="true" width="300" height="300">
		<mx:XMLListCollection id="MailBox">
			<fx:XMLList>
				<folder label="Mail">
					<folder label="INBOX"/>
					<folder label="Personal Folder">
						<Pfolder label="Business" />
						<Pfolder label="Demo" /> 
						<Pfolder label="Personal" isBranch="true" /> 
						<Pfolder label="Saved Mail" /> 
					</folder>
					<folder label="Sent" />
					<folder label="Trash" />
				</folder>
			</fx:XMLList>
		</mx:XMLListCollection>
	</mx:Tree>
	<mx:Button id="button1" x="201" y="308" label="得到选择节点下的所有子节点label值" click="getAllChildLabel()" />
	<mx:Button id="button2" x="201" y="338" label="得到选择节点的所有父节点label值" click="getAllParentLabel()" />
	<mx:TextArea id="textArea1" x="317" y="2" width="276" height="298"/>
	
</s:Application>


另一个例子:
private function recursiveHandler(list:XMLList,currNode:String,templates:XML):XML
{
	for each(var j:XML in list)
	{
		if(currNode==j.derivedFrom)
		{
			var xmlResult:XML = <{j.alias} label={j.alias}/>;
			templates.appendChild(xmlResult);
			delete list[j.childIndex()];
			arguments.callee(list,j.alias,xmlResult);
		}
	}
	return templates;
}



参考 http://topic.csdn.net/u/20100518/10/20978562-cfa5-4264-89eb-d7a8f59afc09.html

你可能感兴趣的:(Flex)