Drag an Image to DataGrid and do calculation based on new added item : DataGrid Drag Drop « Grid « Flex






Drag an Image to DataGrid and do calculation based on new added item

Drag an Image to DataGrid and do calculation based on new added item
       

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="450" height="300">
  <mx:Script>
    
    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 = new ArrayCollection();

    private function dragIt(event:MouseEvent, name:String, price:Number):void {
      var dragInitiator:Image = event.currentTarget as Image;

      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(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:Number(event.dragSource.dataForFormat('price'))});
      total += price;
    } 
  
  </mx:Script>
  <mx:HBox>
  <mx:Image source="logo.jpg" mouseMove="dragIt(event, 'Product', 1.9);" />
  <mx:DataGrid id="cart" dataProvider="{cartContents}" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);" x="175" y="50" height="165">
    <mx:columns>
      <mx:DataGridColumn dataField="name" headerText="Name" />
      <mx:DataGridColumn dataField="price" headerText="Price" />
    </mx:columns>
  </mx:DataGrid> 
  <mx:Label text="Total: $ {total}"/>    
  </mx:HBox>
</mx:Application>

   
    
    
    
    
    
    
  








Related examples in the same category

1.DataGrid accepting dragging and dropping from another DataGridDataGrid accepting dragging and dropping from another DataGrid
2.Handle dragEnter event for DataGridHandle dragEnter event for DataGrid
3.DataGrid drag enter eventDataGrid drag enter event
4.Drag and drop between DataGridDrag and drop between DataGrid
5.Drag and move between DataGridDrag and move between DataGrid
6.Get drag initiator DataGrid from DragEventGet drag initiator DataGrid from DragEvent
7.DataGrid drag and dropDataGrid drag and drop
8.drag and move DataGrid itemsdrag and move DataGrid items
9.DataGrid drag and drop eventDataGrid drag and drop event
10.Drag and drop from List to DataGridDrag and drop from List to DataGrid
11.Drag and drop rows from either DataGrid control to the otherDrag and drop rows from either DataGrid control to the other
12.Move or copy data from a Spark List control to an MX DataGrid control by drag and dropMove or copy data from a Spark List control to an MX DataGrid control by drag and drop