PDA

View Full Version : EXTJS setRootNode doesn't work



cipp
20 Aug 2015, 12:56 PM
Hi, I am trying to create a dynamic tree by the following way:

This is my Store:


Ext.define('Formbuilder.store.Observations', { extend: 'Ext.data.TreeStore',
model: 'Formbuilder.model.Observation',
proxy: {
type: 'memory'
},
root: {
text: 'Root',
id: 'rootNode',
expanded: true
},
});

This is the relevant part of my view:



{
xtype: 'treepanel',
id: 'form-builder-tree',
region: 'center',
store: 'Observations',
title: formbuilder.messages.estructura,
cls: 'buildertree',
tbar: [{
xtype: 'button',
id: 'builder-insetNode',
text: formbuilder.messages.anyadirObservacion,
action: 'insertNode',
margin: "0 0 0 5"
}, {
xtype: 'button',
id: 'builder-deleteNode',
text: formbuilder.messages.borrarObservacion,
action: 'deleteNode',
margin: "0 0 0 5"
}],
viewConfig: {
plugins: {
ptype: 'treeviewdragdrop',
containerScroll: true
}
},
rootVisible: false,
}
........


And this is the relevant part of my controller:



saveProtocol: function(button) {
// Obtenemos el nodo raz
var tree = Ext.getCmp('form-builder-tree');
var rootNode = tree.getRootNode();

console.log(rootNode); // <<-- I get the correct object here


Ext.getStore('Observations').setRootNode(rootNode);


console.log(Ext.getStore('Observations').getRootNode()); // <<-- I get null object here, why??
}


You can appreciate that my tree is using the 'treeviewdragdrop' plugin. The idea is to start with a root node tree, and add nodes dynamically to the tree defined in the view with the following code in the controller:


addNode: function(button) {
// New node to add
var newNode = {
text: 'New',
nombre: 'New',
leaf: true
};


var tree = button.up('treepanel');
var nodeSelected = tree.getSelectionModel().getSelection()[0];


if (!Ext.isEmpty(nodeSelected)) {
if (nodeSelected.isLeaf()) {
nodeSelected.set('leaf', false);
nodeSelected.set('expanded', true);
}
nodeSelected.appendChild(newNode);
} else {
tree.getRootNode().appendChild(newNode);
}
},


This works fine, and I can add and remove every node from the tree. The problem is when I try to save the tree to the store. I am doing the following logic:

1. Customize my tree view adding/removing nodes.
2. Saving my tree view structure to my tree store setting the rootNode of the store to the rootNode of my tree view.

I read in some forums that there was a bug with 'node.copy'. This affect to EXTJS 4.2 and setRootNode method of Tree Store?. Any idea why I get null rootNode when I set the rootNode store to the rootNode of my tree view? or maybe the plugin 'dragdrop' is not updated?

Thanks!.

bjdurham85
16 Sep 2015, 10:32 AM
Hi cipp,

Instead of attempting to setRootNode on the store directly does it work extending from the tree?

Original:


Ext.getStore('Observations').setRootNode(rootNode);


Proposed:

tree.getStore().setRootNode(rootNode);


Regards,
Bryan