Skip to content

Instantly share code, notes, and snippets.

@vitaliy-evsyukov
Created March 15, 2012 12:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vitaliy-evsyukov/2043941 to your computer and use it in GitHub Desktop.
Save vitaliy-evsyukov/2043941 to your computer and use it in GitHub Desktop.
ExtJS 4 Drag&Drop from GridPanel to TreePanel and from TreePanel to GridPanel
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Grid to Tree Drag and Drop Example</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="../shared/example.css" />
<script type="text/javascript" src="../../bootstrap.js"></script>
<script type="text/javascript" src="../shared/examples.js"></script>
<script type="text/javascript" src="dnd_grid_to_tree.js"></script>
</head>
<body>
<h1>Drag and Drop from Grid to Tree Example</h1>
<p>This example shows how to setup two way drag and drop from one GridPanel to TreePanel.</p>
<p>Note that the js is not minified so it is readable. See <a href="dnd_grid_to_tree.js">dnd_grid_to_tree.js</a>.</p>
<div id="panel"></div>
</body>
</html>
/*
This file is part of Ext JS 4
Copyright (c) 2011 Sencha Inc
Contact: http://www.sencha.com/contact
GNU General Public License Usage
This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
*/
Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.dd.*'
]);
Ext.define('DataObject', {
extend: 'Ext.data.Model',
fields: ['text', 'column1', 'column2']
});
Ext.onReady(function(){
var myData = [
{ text : "Rec 0", column1 : "0", column2 : "0" },
{ text : "Rec 1", column1 : "1", column2 : "1" },
{ text : "Rec 2", column1 : "2", column2 : "2" },
{ text : "Rec 3", column1 : "3", column2 : "3" },
{ text : "Rec 4", column1 : "4", column2 : "4" },
{ text : "Rec 5", column1 : "5", column2 : "5" },
{ text : "Rec 6", column1 : "6", column2 : "6" },
{ text : "Rec 7", column1 : "7", column2 : "7" },
{ text : "Rec 8", column1 : "8", column2 : "8" },
{ text : "Rec 9", column1 : "9", column2 : "9" }
];
// create the data store
var firstGridStore = Ext.create('Ext.data.Store', {
model: 'DataObject',
data: myData
});
// Column Model shortcut array
var columns = [
{text: "Record Name", flex: 1, sortable: true, dataIndex: 'text'},
{text: "column1", width: 70, sortable: true, dataIndex: 'column1'},
{text: "column2", width: 70, sortable: true, dataIndex: 'column2'}
];
// declare the source Grid
var firstGrid = Ext.create('Ext.grid.Panel', {
viewConfig: {
plugins: {
ptype: 'gridviewdragdrop',
ddGroup: 'selDD'
},
listeners: {
drop: function(node, data, dropRec, dropPosition) {
var dropOn = dropRec ? ' ' + dropPosition + ' ' + dropRec.get('text') : ' on empty view';
Ext.example.msg("Drag from right to left", 'Dropped ' + data.records[0].get('text') + dropOn);
}
}
},
store : firstGridStore,
columns : columns,
stripeRows : true,
title : 'First Grid',
margins : '0 2 0 0'
});
var secondTreeStore = Ext.create('Ext.data.TreeStore', {
model: 'DataObject',
root: {
text: 'Tree Root',
expanded: true,
children: [
{ text: "detention", leaf: true },
{ text: "homework", expanded: true, children: [
{ text: "book report", leaf: true },
{ text: "alegrbra", leaf: true}
] },
{ text: "buy lottery tickets", leaf: true }
]
}
});
// create the destination Grid
var secondTree = Ext.create('Ext.tree.Panel', {
viewConfig: {
plugins: {
ptype: 'treeviewdragdrop',
ddGroup: 'selDD'
},
listeners: {
beforedrop: function(node, data) {
data.records[0].set('leaf', true);
data.records[0].set('checked', null);
console && console.log(arguments);
},
drop: function(node, data, dropRec, dropPosition) {
var dropOn = dropRec ? ' ' + dropPosition + ' ' + dropRec.get('text') : ' on empty view';
Ext.example.msg("Drag from left to right", 'Dropped ' + data.records[0].get('text') + dropOn);
firstGrid.store.remove(data.records[0]);
}
}
},
store : secondTreeStore
});
//Simple 'border layout' panel to house both grids
var displayPanel = Ext.create('Ext.Panel', {
width : 650,
height : 300,
layout : {
type: 'hbox',
align: 'stretch',
padding: 5
},
renderTo : 'panel',
defaults : { flex : 1 }, //auto stretch
items : [
firstGrid,
secondTree
],
dockedItems: {
xtype: 'toolbar',
dock: 'bottom',
items: ['->', // Fill
{
text: 'Reset both components',
handler: function(){
//refresh source grid
firstGridStore.loadData(myData);
//purge destination grid
secondTreeStore.removeAll();
}
}]
}
});
});
@perryflynn
Copy link

This is pretty cool.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment