< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
< link rel ="stylesheet" type ="text/css" href ="files/ext-all.css" >
< script type ="text/javascript" src ="files/ext-base.js" ></ script >
<script type= "text/javascript" src= "files/ext-all.js" ></script>
<script type= "text/javascript" src= "files/grid2treedrag.js" > </ script >
< title id ="page-title" > Drag&Drop TreePanel GridPanel ExtJS </ title >
</ head >
< body >
< h3 > Drag&Drop TreePanel GridPanel ExtJS </ h3 >
</ body >
</ html >
* This source code was highlighted with Source Code Highlighter .
var grid1 = new Ext.grid.GridPanel({
store: new Ext.data.ArrayStore({
fields: [ 'name' , 'unit' , 'price' ],
data: d
}),
columns:[
{
id: 'name_column' ,
header: "" ,
width:40,
sortable: true ,
dataIndex: 'name'
},{
id: 'unit_column' ,
header: ". ." ,
width:20,
sortable: true ,
dataIndex: 'unit'
},
{
id: 'price_column' ,
header: "" ,
width:30,
sortable: true ,
dataIndex: 'price'
}
],
sm : sm,
viewConfig:{
forceFit: true
},
id: 'grid' ,
title: '' ,
region: 'center' ,
layout: 'fit' ,
enableDragDrop: true ,
ddGroup: 'grid2tree'
});
* This source code was highlighted with Source Code Highlighter .
var tree = new Ext.tree.TreePanel({
root:{
text: '' ,
id: 'root' ,
expanded: true ,
children:[{
text: '' ,
children:[{
text: '' ,
leaf: true ,
price: 7000,
unit: ''
}, {
text: '' ,
leaf: true ,
price: 5400,
unit: ''
}, {
text: ' ' ,
children:[{
text: '' ,
leaf: true ,
price: 7000,
unit: ''
}]
}]
},{
text: ' ' ,
children:[{
text: '' ,
leaf: true ,
price: 800,
unit: ''
}]
},{
text: ' ++' ,
leaf: true ,
price: 1200,
unit: ''
}]
},
loader: new Ext.tree.TreeLoader({
preloadChildren: true
}) ,
enableDD: true ,
ddGroup: 'grid2tree' ,
id: 'tree' ,
region: 'east' ,
title: ' ' ,
layout: 'fit' ,
width:300,
split: true ,
collapsible: true ,
autoScroll: true ,
listeners:{
beforenodedrop: function (e) {
if (Ext.isArray(e.data.selections)) {
if (e.target == this .getRootNode()) {
return false ;
}
e.cancel = false ;
e.dropNode = [];
var r;
for ( var i = 0; i < e.data.selections.length; i++) {
r = e.data.selections[i];
e.dropNode.push( this .loader.createNode({
text:r.get( 'name' ),
leaf: true ,
price:r.get( 'price' ),
unit: r.get( 'unit' )
}));
r.store.remove(r);
}
return true ;
}
}
}
});
* This source code was highlighted with Source Code Highlighter .
if (e.target == this.getRootNode()) {
return false;
}
var cb = new Ext.FormPanel({
region: 'south' ,
frame: true ,
height: 40,
labelWidth: 200,
labelPad: 0,
items: [
{
xtype: 'checkbox' ,
fieldLabel: ' ' ,
listeners: {
check: function (cb, checked ) {
remove_catalogs = checked ;
}
}
}
]
});
// create and show the window
var win = new Ext.Window({
title: ' ' ,
id: 'tree2divdrag' ,
border: false ,
layout: 'border' ,
width:700,
height:400,
items:[tree, grid1, cb]
});
win.show();
* This source code was highlighted with Source Code Highlighter .
var gridTargetEl = grid1.getView().scroller.dom;
ts.master = new Ext.Template(
'<div class="x-grid3" hidefocus="true">' ,
'<div class="x-grid3-viewport">' ,
'<div class="x-grid3-header"><div class="x-grid3-header-inner"><div class="x-grid3-header-offset" style="{ostyle}">{header}</div></div><div class="x-clear"></div></div>' ,
'<div class="x-grid3-scroller"><div class="x-grid3-body" style="{bstyle}">{body}</div><a href="#" class="x-grid3-focus" tabIndex="-1"></a></div>' ,
'</div>' ,
'<div class="x-grid3-resize-marker"> </div>' ,
'<div class="x-grid3-resize-proxy"> </div>' ,
'</div>'
);
* This source code was highlighted with Source Code Highlighter .
var GridDropTarget = new Ext.dd.DropTarget(gridTargetEl, {
ddGroup : 'grid2tree' ,
notifyDrop : function (ddSource, e, data) {
e.cancel = false ;
var node = ddSource.dragData.node;
if ( ( (node.parentNode == null ) || (!node.isLeaf() && !remove_catalogs) ) && !node.hasChildNodes() ) {
e.cancel = true ;
return false ;
}
var r = [];
if (!node.isLeaf()) {
node.cascade( function (n) {
var x = populate(n);
if (x != -1)
r.push(x);
});
}
else
r = populate(node);
grid1.store.add(r);
if ( (node.parentNode != null ) && (remove_catalogs || !node.hasChildNodes()) ) {
node.remove();
}
else {
removeChildNodes(node);
}
return true ;
}
});
* This source code was highlighted with Source Code Highlighter .
var populate = function (node) {
if (!node.isLeaf()) return -1;
var r = new Ext.data.Record();
r.data.name = node.text;
r.data.price = node.attributes.price;
r.data.unit = node.attributes.unit;
return r;
}
* This source code was highlighted with Source Code Highlighter .
var removeChildNodes = function (node) {
node.expand();
for ( var i = node.childNodes.length - 1; i >= 0; i--) {
var currentNode = node.childNodes[i];
if (currentNode.isLeaf() || remove_catalogs)
node.removeChild(currentNode);
else
removeChildNodes(currentNode);
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/111236/
All Articles