PDA

View Full Version : Tree - Copy Nodes instead of moving



mcohnen
26 Nov 2007, 3:33 AM
Hi,
I want nodes to copied instead of moved when dragging them in a Tree. I have searched in the forums and found a solution, but it is not working in 2.0

Copying instead of moving in 1.x (http://extjs.com/forum/showthread.php?t=2683)

I have looked into the api and don't see a config option or something similar.

Has someone already made it?

Any help would be appreciated.

Thank you!

para
26 Nov 2007, 9:21 AM
tree.on('nodedrop', function(e) {
if(shouldBeCopy) { //replace shouldBeCopy with whatever condition you need
// should be something like this:
e.dropNode = new Ext.tree.TreeNode(e.dropNode);
}
});


I have a TreeNode.clone() function implemented because my tree nodes are a bit more complicated than the Ext2 originals.

joe123
12 May 2010, 4:12 AM
Hi,

Could you please share your clone method?
I am facing the same problem and it would
save me a lot of time.

Thanks!

para
12 May 2010, 8:27 AM
This will not work out-of-the-box. You will have to edit it to remove my custom stuff, like getConfig(), 'form', .cls stuff, etc.

Hope it helps though!


clone: function() {
//forces creation of a new id by removing the original.
var atts = this.attributes;
atts.id = Ext.id(null, "ynode-");
var config = this.getConfig();

if(this.getType() == 'form' && this.ui && this.ui.getEl()) {
atts.cls = config.cls;
if(Ext.get(this.ui.getEl().firstChild).hasClass(config.cls2)) {
atts.cls = config.cls2;
}
}

if(this.childrenRendered || this.loaded || !this.attributes.children) {
atts.children = null;
var newNode = new Ext.tree.TreeNode(Ext.apply({}, atts));
}
else {
var newAtts = Ext.apply({}, atts);
newAtts.children = this.cloneUnrenderedChildren();
var newNode = new Ext.tree.AsyncTreeNode(newAtts);
}
newNode.text = this.text;

if(this.hasData()) {
if(this.getData().clone) {
newNode.setData(this.getData().clone());
}
else {
newNode.setData(this.getData());
}
}

for(var i=0; i<this.childNodes.length; i++){
newNode.appendChild(this.childNodes[i].clone());
}
newNode.leaf = false;
return newNode;
},


cloneUnrenderedChildren: function() {
unrenderedClone = function(n) {
n.id = undefined;
if(n.children) {
for(var j=0; j<n.children.length; j++) {
n.children[j] = unrenderedClone(n.children[j]);
}
}
return n;
};
var c = [];
for(var i=0; i<this.attributes.children.length; i++) {
c[i] = Ext.apply({}, this.attributes.children[i]);
c[i] = unrenderedClone(c[i]);
}
return c;
},