<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>VinylFox &#187; ExtJS 2.x</title>
	<atom:link href="http://www.vinylfox.com/tag/extjs-2x/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vinylfox.com</link>
	<description>The Playground of VinylFox (Shea Frederick)</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:26:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Getting Started With Grid Drag &amp; Drop</title>
		<link>http://www.vinylfox.com/getting-started-with-grid-drag-drop/</link>
		<comments>http://www.vinylfox.com/getting-started-with-grid-drag-drop/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 17:32:27 +0000</pubDate>
		<dc:creator>Shea Frederick</dc:creator>
				<category><![CDATA[ExtJS Tutorials]]></category>
		<category><![CDATA[ExtJS 2.x]]></category>

		<guid isPermaLink="false">http://www.vinylfox.com/?p=113</guid>
		<description><![CDATA[This tutorial builds on the demo code included with the Learning ExtJS book, specifically the 7_json_grid.php example from chapter 5 &#8211; which can also be downloaded here. The code for this tutorial can be downloaded here, and a live example is available here. Were going to implement a fairly simplistic grid drag and drop. This [...]]]></description>
			<content:encoded><![CDATA[<p><b>This tutorial builds on the demo code included with the Learning ExtJS book, specifically the 7_json_grid.php example from chapter 5 &#8211; which can also be <a href="http://code.google.com/p/learning-extjs-book-samples/source/browse/trunk/Chapter5/7_json_grid.php">downloaded here.</a></b></p>
<p>The code for this tutorial can be downloaded <a href="http://www.vinylfox.com/examples/grid-drag-and-drop.zip">here</a>, and a live example is available <a href="http://www.vinylfox.com/examples/grid-drag-and-drop/grid-drag-and-drop.php">here</a>.</p>
<p>Were going to implement a fairly simplistic grid drag and drop. This implementation will allow you to drag a row of data to re-order it within the same grid. We will also play with the look of the <i>dragproxy</i>, which is a visual element used to represent the grid row while its in transition.</p>
<h3>Getting Started</h3>
<p>The first thing we need to do is tell our <i>GridPanel</i> that it&#8217;s going to be used for drag and drop, this is because the default behavior for a grid is to have no drag and drop functionality (which cuts down on resources, ie: memory). We can enable the drag and drop functionality by setting the <i>enableDragDrop</i> config value to <em>true</em></p>
<p><textarea class="js" cols="100" rows="15" name="code">enableDragDrop : true</textarea></p>
<p>Now that the first step is out of the way, lets move on to defining some of our drag and drop specifics.</p>
<h3>D&#038;D Groups</h3>
<p>We will need to set the drag and drop group &#8211; <i>ddGroup</i> &#8211; this is a unique identifier that ExtJS components will use to share their dragged and dropped items. For our example, we are using &#8216;mygrid-dd&#8217;. In your case this might be something more along the lines of &#8216;contacts&#8217; or &#8216;products&#8217; which could be set on multiple components to allow drag and drop between them.</p>
<p><textarea class="js" cols="100" rows="15" name="code">ddGroup : &#8216;mygrid-dd&#8217;,<br />
ddText : &#8216;Place this row.&#8217;,</textarea></p>
<p><a href="http://www.vinylfox.com/wp-content/uploads/2008/12/ddgrid-start.png"><img style="margin-right: 15px;" src="http://www.vinylfox.com/wp-content/uploads/2008/12/ddgrid-start-300x230.png" alt="ddgrid-start" title="ddgrid-start" width="300" height="230" class="alignnone size-medium wp-image-156" border=0 align="left"/></a>Setting those two config options will give us a grid that allows rows to be dragged but not dropped, and has a very generic looking <i>dragproxy</i>. So lets first tackle getting rid of that ugly <i>dragproxy</i> by overwriting it at the start of the selection process. We can do this by adding a <i>beforerowselect</i> listener that will modify the <i>dragproxy</i> message.</p>
<h3>Listening For The Drag</h3>
<p>Using the grids selection model, we can listen for a <i>beforerowselect</i> event and change the <i>dragproxy</i>.</p>
<p><textarea class="js" cols="100" rows="15" name="code">sm: new Ext.grid.RowSelectionModel({<br />
    singleSelect:true,<br />
    listeners: {<br />
        beforerowselect: function(sm,i,ke,row){<br />
            grid.ddText = title_img(row.data.title, null, row);<br />
        }<br />
    }<br />
})</textarea></p>
<p><img style="margin-right: 15px;" src="http://www.vinylfox.com/wp-content/uploads/2009/01/custom-dragproxy.png" alt="custom-dragproxy" title="custom-dragproxy" width="280" height="155" align="left" class="alignleft size-full wp-image-177" />This gives us a much nicer looking <i>dragproxy</i>, but we still have nowhere to drop the row.</p>
<h3>Dropping The Row</h3>
<p>In order to drop a row we need to setup the grids body area as a <i>DropTarget</i>, which will allow it to accept a dropped row.</p>
<p><textarea class="js" cols="100" rows="15" name="code">var ddrow = new Ext.dd.DropTarget(grid.getView().mainBody, {<br />
    ddGroup : &#8216;mygrid-dd&#8217;,<br />
    notifyDrop : function(dd, e, data){<br />
        var sm = grid.getSelectionModel();<br />
        var rows = sm.getSelections();<br />
        var cindex = dd.getDragData(e).rowIndex;<br />
        if (sm.hasSelection()) {<br />
            for (i = 0; i < rows.length; i++) {<br />
                store.remove(store.getById(rows[i].id));<br />
                store.insert(cindex,rows[i]);<br />
            }<br />
            sm.selectRecords(rows);<br />
        }<br />
    }<br />
});</textarea></p>
<p>Just like the <i>GridPanel</i>, one of the first config options we need to set on the <i>DropTarget</i> is the <i>ddGroup</i>, which is set to <i>mygrid-dd</i>. This allows the <i>GridPanel</i> to accept any drops that come from a component having this same <i>ddGroup</i> (of course we are only using one as both source and target). Next we setup the code to be executed when a successful drop happens &#8211; the <i>notifyDrop</i>.</p>
<p>Lets break down the code were executing when a successful drop occurs:</p>
<ol>
<li>Grab a reference to the <i>GridPanel</i>&#8216;s <i>SelectionModel</i>.</li>
<li>Find out what row(s) are selected.</li>
<li>Get the index of where the row was dropped in relation to the other rows.</li>
<li>Loop through the selected rows.</li>
<li>Remove the row of data from its current location.</li>
<li>Add the row of data at its new location.</li>
<li>Select the new row (just for a touch of class)</li>
</ol>
<p>From this bit of code we have a grid that allows you to re-order the rows. If you wanted to update the database with the new order, then an ajax call could be added within the <i>notifyDrop</i> event. We could even use this same basic concept to drag rows between separate grids or other ExtJS components.</p>
<h3>Whats Next?</h3>
<p>There are a few samples in the SDK download that deal with drag and drop, and as always the API documentation has a huge amount of information to help you on your way.</p>
<ul>
<li><a href="http://www.extjs.com/deploy/dev/examples/tree/reorder.html">Reorder Within a Tree</a></li>
<li><a href="http://www.extjs.com/deploy/dev/examples/tree/two-trees.html">Multiple Tree Drag &#038; Drop</a></li>
<li><a href="http://www.extjs.com/deploy/dev/examples/dd/dnd_grid_to_grid.html">Multiple Grid Drag &#038; Drop</a></li>
<li><a href="http://extjs.com/deploy/dev/docs/">API Documentation</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.vinylfox.com/getting-started-with-grid-drag-drop/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

