Flex 购物车拖拽

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="400" backgroundColor="#FFFFFF" creationComplete="initApp()" layout="absolute">
  <mx:Script>
      <![CDATA[
             import mx.controls.DataGrid;
                import mx.controls.Image;
                import mx.collections.ArrayCollection;
                import mx.events.DragEvent;
                import mx.managers.DragManager;
                import mx.core.DragSource;
                [Bindable]     
                public var total:Number=0;     
                [Bindable]     
                public var cartContents:ArrayCollection;     
                private function initApp():void{       
                this.cartContents=new ArrayCollection();      }     
                private function dragIt(event:MouseEvent,name:String,price:Number):void{       
               
                //CurrentTarget指定要实现拖拽事件的初始化目标       
                var dragInitiator:Image=event.currentTarget as Image;       
                //指定一个dragSource来包括拖拽过程中包含的数据的对象       
                var dragSource:DragSource=new DragSource();        //向对象添加数据       
                dragSource.addData(name,'name');       
                dragSource.addData(price,'price');       
                //创建一个拖拽对象的代理作为拷贝       
                var dragProxy:Image=new Image();      
                dragProxy.source=event.currentTarget.source;      
                  //使用DragManager静态方法doDrag开始拖拽       
                DragManager.doDrag(dragInitiator,dragSource,event,dragProxy);     
                }     
                private function dragEnterHandler(event:DragEvent):void{       
                var dropTarget:DataGrid=event.currentTarget as DataGrid;       
                if (event.dragSource.hasFormat('name') && event.dragSource.hasFormat('price'))
                {          DragManager.acceptDragDrop(dropTarget);        }      }    
                 private function dragDropHandler(event:DragEvent):void{       
                
                var name:String= String(event.dragSource.dataForFormat('name'));       
                var price:Number=Number(event.dragSource.dataForFormat('price'));       
                this.cartContents.addItem({name:String(event.dragSource.dataForFormat('name')),price:String(event.dragSource.dataForFormat('price'))});
                        total+=price;     
                }   
                ]]> 
             </mx:Script> 
 <mx:Canvas x="19" y="10"> 
 <mx:Image x="23" y="35" width="64" height="64" mouseMove="dragIt(event,'Dreamweaver',499);" source="46.png"/> 
 <mx:Label x="41" y="107" text="499"/> 
 <mx:Image x="23" y="133" width="64" height="64" mouseMove="dragIt(event,'Fireworks',299);"  source="46.png"/> 
 <mx:Label x="41" y="205" text="299"/> 
 <mx:Image x="23" y="231" width="64" height="64" mouseMove="dragIt(event,'Flash',599);" source="46.png"/> 
 <mx:Label x="41" y="303" text="599"/>  </mx:Canvas> 
 <mx:Label x="210" y="61" text="购物篮" fontSize="12"/> 
 <mx:DataGrid x="129.5" y="102" id="cart" dataProvider="{cartContents}" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);" height="165" fontSize="12">   
 <mx:columns>     
 <mx:DataGridColumn headerText="产品" dataField="name"/>     
 <mx:DataGridColumn headerText="价格" dataField="price"/>   
 </mx:columns> 
 </mx:DataGrid> 
 <mx:Label x="186" y="292" text="总计:{total}" fontSize="12"/> 
 <mx:Label x="129.5" y="0" text="拖拽物品放入购物篮中" fontSize="12"/> 
 <mx:HRule x="5" y="20" width="390"/>
</mx:Application>

本例子 是从网上Copy的具体出处忘了

System.totalMemory 在 IE 下面会误报,一定要在 FF 或 SA 下面用 写程序要养成好习惯

 

posted @ 2010-09-05 21:18  只身走天下  阅读(397)  评论(0)    收藏  举报