Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Getting Started With Grid Drag & Drop (0 Comments)
Admin: Posted Date: April 4, 2010

 The first thing we need to do is tell our GridPanel that it'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 enableDragDrop config value to true

Getting Started With Grid Drag & Drop

Getting Started

The first thing we need to do is tell our GridPanel that it'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 enableDragDrop config value to true

 

  1. enableDragDrop : true  
enableDragDrop : true

Now that the first step is out of the way, lets move on to defining some of our drag and drop specifics.

D&D Groups

We will need to set the drag and drop group - ddGroup - this is a unique identifier that ExtJS components will use to share their dragged and dropped items. For our example, we are using 'mygrid-dd'. In your case this might be something more along the lines of 'contacts' or 'products' which could be set on multiple components to allow drag and drop between them.

  1. ddGroup : 'mygrid-dd',  
  2. ddText : 'Place this row.',  
ddGroup : 'mygrid-dd', ddText : 'Place this row.',

ddgrid-startSetting those two config options will give us a grid that allows rows to be dragged but not dropped, and has a very generic looking dragproxy. So lets first tackle getting rid of that ugly dragproxy by overwriting it at the start of the selection process. We can do this by adding a beforerowselect listener that will modify the dragproxy message.

Listening For The Drag

Using the grids selection model, we can listen for a beforerowselect event and change the dragproxy.

  1. sm: new Ext.grid.RowSelectionModel({  
  2.     singleSelect:true,  
  3.     listeners: {  
  4.         beforerowselect: function(sm,i,ke,row){  
  5.             grid.ddText = title_img(row.data.title, null, row);  
  6.         }  
  7.     }  
  8. })  
sm: new Ext.grid.RowSelectionModel({ singleSelect:true, listeners: { beforerowselect: function(sm,i,ke,row){ grid.ddText = title_img(row.data.title, null, row); } } })

custom-dragproxyThis gives us a much nicer looking dragproxy, but we still have nowhere to drop the row.

Dropping The Row

In order to drop a row we need to setup the grids body area as a DropTarget, which will allow it to accept a dropped row.

                   

  1. var ddrow = new Ext.dd.DropTarget(grid.getView().mainBody, {  
  2.     ddGroup : 'mygrid-dd',  
  3.     notifyDrop : function(dd, e, data){  
  4.         var sm = grid.getSelectionModel();  
  5.         var rows = sm.getSelections();  
  6.         var cindex = dd.getDragData(e).rowIndex;  
  7.         if (sm.hasSelection()) {  
  8.             for (i = 0; i < rows.length; i++) {  
  9.                 store.remove(store.getById(rows[i].id));  
  10.                 store.insert(cindex,rows[i]);  
  11.             }  
  12.             sm.selectRecords(rows);  
  13.         }    
  14.     }  
  15. });  
var ddrow = new Ext.dd.DropTarget(grid.getView().mainBody, { ddGroup : 'mygrid-dd', notifyDrop : function(dd, e, data){ var sm = grid.getSelectionModel(); var rows = sm.getSelections(); var cindex = dd.getDragData(e).rowIndex; if (sm.hasSelection()) { for (i = 0; i < rows.length; i++) { store.remove(store.getById(rows[i].id)); store.insert(cindex,rows[i]); } sm.selectRecords(rows); } } });

Just like the GridPanel, one of the first config options we need to set on the DropTarget is the ddGroup, which is set to mygrid-dd. This allows the GridPanel to accept any drops that come from a component having this same ddGroup (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 - the notifyDrop.

Lets break down the code were executing when a successful drop occurs:

  1. Grab a reference to the GridPanel's SelectionModel.
  2. Find out what row(s) are selected.
  3. Get the index of where the row was dropped in relation to the other rows.
  4. Loop through the selected rows.
  5. Remove the row of data from its current location.
  6. Add the row of data at its new location.
  7. Select the new row (just for a touch of class)

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 notifyDrop event. We could even use this same basic concept to drag rows between separate grids or other ExtJS components.

 

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: