Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

Flex Drag and Drop Custom Class Source Code Example – mx.managers.DragManager

Posted on March 7, 2010 | No Comments

Flex Drag and Drop Custom Class Source Code Example:

classdragdrop .mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application   xmlns:mx="http://www.adobe.com/2006/mxml"   backgroundAlpha="0"   xmlns="*" creationComplete="initApp()">

	<mx:Script>

		import mx.core.*;
		import   mx.managers.DragManager;
		import mx.events.DragEvent;
		import mx.collections.ArrayCollection;

		[Bindable]
		public       var   cart : ArrayCollection;

		private function   initApp() : void 
		{
			thumbnail.product={name:   'Siemens SX1', price: 99.99, image: 'assets/products/Siemens_SX1.png'};
			cart = new ArrayCollection();
		}

		private   function doDragEnter(event:DragEvent):void
		{
			if (event.dragSource.hasFormat("item"))
			{
				DragManager.acceptDragDrop(IUIComponent(event.target));
				event.preventDefault();
			}
		}

		private     function doDragDrop(event:DragEvent):void
		{
			var item:Object   = event.dragSource.dataForFormat("item");
			cart.addItem(item);
			event.preventDefault();
		}

	</mx:Script>

<mx:Label     text="Drag   the product thumbnail and drop   it in the datagrid"/>

<mx:HBox horizontalGap="30" height="100%">

	<Thumbnail id="thumbnail"/>

	&lt;mx:Panel   title=&quot;Shopping Cart&quot; width=&quot;300&quot; <a href="http://over50losingweight.com/images/">where to buy cialis without prescription</a>  height=&quot;100%&quot; borderAlpha=&quot;1&quot;   paddingTop=&quot;20&quot;&gt;
		&lt;mx:DataGrid   id=&quot;dg&quot;   dataProvider=&quot;{cart}&quot;   width=&quot;100%&quot; height=&quot;100%&quot;
		dragEnter=&quot;doDragEnter(event)&quot;
		dragDrop=&quot;doDragDrop(event)&quot;&gt;
		&lt;mx:columns&gt;
			&lt;mx:Array&gt;
				&lt;mx:DataGridColumn         dataField=&quot;name&quot;   headerText=&quot;Name&quot;/&gt;
				&lt;mx:DataGridColumn dataField=&quot;price&quot; headerText=&quot;Price&quot;/&gt;
			&lt;/mx:Array&gt;
		&lt;/mx:columns&gt;
		&lt;/mx:DataGrid&gt;
	&lt;/mx:Panel&gt;

&lt;/mx:HBox&gt;

&lt;/mx:Application&gt;

thumnail.xml

&lt;?xml       version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;mx:HBox xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
paddingTop=&quot;4&quot;   paddingBottom=&quot;4&quot;       paddingLeft=&quot;4&quot; paddingRight=&quot;4&quot;
borderStyle=&quot;solid&quot; verticalAlign=&quot;middle&quot; <a href="http://jtc-enterprises.com/images/">buy cialis tadalafil</a>  backgroundColor=&quot;#FFFFFF&quot;
width=&quot;170&quot;   height=&quot;130&quot;
horizontalScrollPolicy=&quot;off&quot;   verticalScrollPolicy=&quot;off&quot;
mouseMove=&quot;beginDrag(event)&quot;&gt;

&lt;mx:Script&gt;

	import mx.core.*;
	import   mx.managers.DragManager;

	[Bindable]
	public     var product:Object;

	public function beginDrag(event:MouseEvent)   : void
	{
		var ds:DragSource   = new DragSource();
		ds.addData(product, &quot;item&quot;);
		var   proxy:Thumbnail   = new Thumbnail(); //The drag proxy
		proxy.product = product;
		DragManager.doDrag(this, ds, event, proxy, 16-mouseX, -mouseY, 0.5,   false);
	}

&lt;/mx:Script&gt;

&lt;mx:Image id=&quot;img&quot; source=&quot;../{product.image}&quot; width=&quot;50&quot; height=&quot;100&quot;/&gt;
&lt;mx:VBox width=&quot;100%&quot; height=&quot;100%&quot;   verticalAlign=&quot;middle&quot;&gt;
&lt;mx:Label text=&quot;{product.name}&quot;   fontSize=&quot;11&quot; fontWeight=&quot;bold&quot;/&gt;
&lt;mx:Label text=&quot;Price: {product.price}&quot;/&gt;
&lt;/mx:VBox&gt;

&lt;/mx:HBox&gt;

Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Leave a Reply

You must be logged in to post a comment.