返回顶部

收藏

Filtering Data Example

更多
<?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=”@Embed(source=’../assets/056.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=”@Embed(source=’../assets/057.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=”@Embed(source=’../assets/059.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>
//该片段来自于http://outofmemory.cn

标签:flash,ActionScript,基础

收藏

0人收藏

支持

0

反对

0

发表评论