关闭

Flex tree 限制节点同级间拖拽

499人阅读 评论(0) 收藏 举报
分类:
<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">     
	   <mx:Script> 
		   <![CDATA[                   
		   import mx.events.DragEvent;     
		   import mx.managers.DragManager;      
		   import mx.core.DragSource;          
		   import mx.core.UIComponent;          
		   import mx.controls.Tree;              
		   private var _draggedItem:XML = null;   
		   private function onDragEnter( event:DragEvent ) : void    
		   {                 
			   event.preventDefault();        
			   event.currentTarget.hideDropFeedback(event);  
			   var ds:DragSource = event.dragSource;         
			   var items:Array = ds.dataForFormat("treeItems") as Array;  
			   if (items != null && items.length > 0 && (items[0] is XML))     
				   _draggedItem = items[0];                       
			   DragManager.acceptDragDrop(UIComponent(event.currentTarget));     
		   }                   
			   private function onDragOver( event:DragEvent ) : void    
			   {                 
				   event.preventDefault();                
				   event.currentTarget.hideDropFeedback(event);        
				   tree.selectedIndex = tree.calculateDropIndex(event);    
				   var node:XML = tree.selectedItem as XML;                 
				   // restrict drag & drop to nodes within same parent     
				   if (_draggedItem.parent() != node.parent())              
				   {                   
					   DragManager.showFeedback(DragManager.NONE);    
					   
					   return;          
				   }                      
				   DragManager.showFeedback(DragManager.MOVE);
			   }    
			   
			   private function onDragDrop( event:DragEvent ) : void    
			   {                
				   event.preventDefault();             
				   event.currentTarget.hideDropFeedback(event);     
				   tree.selectedIndex = tree.calculateDropIndex(event); 
				   var node:XML = tree.selectedItem as XML;                        
				   var addToIndex:int = node.childIndex();         
				   if ((_draggedItem.parent() == node.parent()) && (addToIndex != _draggedItem.childIndex()))          
				   {   
					   tree.dataDescriptor.removeChildAt(node.parent(), _draggedItem, _draggedItem.childIndex());       
					   tree.dataDescriptor.addChildAt(node.parent(), _draggedItem, addToIndex);            
				   }       
			   }                    
			   private function onDragComplete( event:DragEvent ) : void           
			   {                
				   tree.selectedIndex = -1;      
			   }            
		   ]]>   
	   </mx:Script>       
	<mx:XML id="treeData" xmlns="">     
		<root>             
			<node label="Massachusetts" type="state" data="MA">    
				<node label="Boston" type="city" >             
					<node label="Smoke House Grill" type="restaurant" />      
					<node label="Equator" type="restaurant" />              
					<node label="Aquataine" type="restaurant" />               
					<node label="Grill 23" type="restaurant" />              
				</node>                
				<node label="Provincetown" type="city" > 
					<node label="Lobster Pot" type="restaurant" />             
					<node label="The Mews" type="restaurant" />             
				</node>           
				</node>      
			<node label="California" type="state" data="CA">   
				<node label="San Francisco" type="city" >        
					<node label="Frog Lane" type="restaurant" />        
				</node>        
			</node>      
		</root>   
	</mx:XML>
   <mx:Tree width="100%" height="100%" id="tree"    
			labelField="@label"   
			dataProvider="{treeData.node}"      
			allowMultipleSelection="false"   
			dragEnabled="true"      
			dropEnabled="true"       
			dragMoveEnabled="false" 
			dragEnter="onDragEnter(event)"      
			dragOver="onDragOver(event)"      
			dragDrop="onDragDrop(event)"      
			dragComplete="onDragComplete(event)"/>  
</mx:Application>


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:68635次
    • 积分:1597
    • 等级:
    • 排名:千里之外
    • 原创:88篇
    • 转载:21篇
    • 译文:5篇
    • 评论:1条
    文章分类
    最新评论