Drag & Drop to a DustBin & increase count in Datagrid


----------------------------------DragandDrop.MXML-------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" 
	width="100%" height="100%" backgroundColor="white">
	<mx:Label x="0" y="210" text="Games"
    width="118" height="42" fontSize="28" color="#818181"/>
    <mx:Style source="styles.css"/>
    <mx:Script>
  <![CDATA[
  	import mx.collections.ArrayCollection;
  	import mx.events.DragEvent;
  	import mx.core.DragSource;
  	import mx.controls.Image;
  	import mx.managers.DragManager;

  [Embed("assets/bioshock_small.png")]
  private var bioshock_icon:Class;

  [Embed("assets/crysis_small.png")]
  private var crysis_icon:Class;

  [Embed("assets/halo_small.png")]
  private var halo_icon:Class;

  [Embed("assets/neverwinternights_small.png")]
  private var neverwinter_icon:Class;

  [Embed("assets/wow_small.png")]
  private var wow_icon:Class;

  [Embed("assets/baskets.PNG")]
  private var basket:Class;
  [Bindable]
 private var cartContents:ArrayCollection = new ArrayCollection();
 private function doDrag(event:MouseEvent):void
 {
  var img:Image = event.currentTarget as Image;
  var dragImg:Image = new Image();
  dragImg.source = img.source;
  var dsource:DragSource = new DragSource();
  dsource.addData(img, 'img');
  DragManager.doDrag(img, dsource, event, dragImg);
 }
private function dragAccept(event:DragEvent):void
{
  var dropTarget:Canvas = event.currentTarget as Canvas;
  DragManager.acceptDragDrop(dropTarget);
}
private function dragDrop(event:DragEvent):void
{
  var img:Image = event.dragSource.dataForFormat('img') as Image;
  addToCart(img);
}
private function addToCart(img:Image):void
{
  for(var i:int = 0; i <cartContents.length; i++)
  {
    if(cartContents[i].name == img.name)
    {
      cartContents[i].num++;
      return;
    }
  }
  cartContents.addItem(new Game(img.name,1));
     <mx:Image name="img1" x="0" y="0" source="{basket}" width="100" height="200"/>
}
  ]]>
</mx:Script>
<mx:HBox width="100%" horizontalCenter="0">
  <mx:Image name="Bioshock" x="2" y="2"
    source="{bioshock_icon}" mouseDown="doDrag(event)" />
<mx:Image name="Crysis" x="120" y="2"
    source="{crysis_icon}" mouseDown="doDrag(event)" />
  <mx:Image name="Halo" x="340" y="2"
    source="{halo_icon}" mouseDown="doDrag(event)" />
  <mx:Image name="Neverwinter Nights" x="50" y="200"
    source="{neverwinter_icon}" mouseDown="doDrag(event)" />
  <mx:Image name="World of Warcraft" x="200" y="110"
    source="{wow_icon}" mouseDown="doDrag(event)" />
</mx:HBox>
  <mx:Label x="272" y="235" text="Shopping Cart" color="#3A3A3A"
    fontWeight="bold" fontSize="12"/>
<mx:HBox>
	<mx:Canvas width="100" height="100" styleName="GradientCanvas">
	</mx:Canvas>
  <mx:Canvas  width="175" height="150" styleName="GradientCanvas"
    borderStyle="solid" backgroundColor="#12976A"
    dragEnter="dragAccept(event)" horizontalScrollPolicy="off" verticalScrollPolicy="off"
    dragDrop="dragDrop(event)" >
  <mx:Image id="i1" name="img1" source="{basket}" width="175" height="195"/>
  </mx:Canvas>
  <mx:DataGrid dataProvider="{cartContents}" x="173" y="255" width="201" height="151">
    <mx:columns>
      <mx:DataGridColumn headerText="Name" dataField="name"/>
      <mx:DataGridColumn headerText="Quantity" dataField="num" width="65"/>
    </mx:columns>
  </mx:DataGrid>
</mx:HBox>
</mx:Application>
-----------------------------------Game.as--------------------------------------------
package
{
  [Bindable]
  public class Game
  {
    public var name:String;
    public var num:Number;

    public function Game(name:String, num:Number)
    {
      this.name = name;
      this.num = num;
    }
  }
}// ActionScript file
Advertisements

~ by mjcprasad2000 on January 16, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: