Ronaldo
31 Jul 2009, 4:59 AM
Hi all,
This is not a real extension, but more a tip: I needed a local tree that does not load data from the server, but is loaded from an array client side.
AFAIK, the TreePanel is configured to always go to the server to request data, which is not what I wanted.
Now the treeLoader, nor the ASyncTreeLoader have a mode='local' property, like the Store.
So here's my solution to use a 'mode=local' tree:
MyTree = Ext.extend(Ext.tree.TreePanel, {
layout : 'fit',
initComponent : function() {
this.root = {
id :'root',
text :'Root',
draggable :false,
type :'folder',
expand :true,
leaf: false,
loaded:true
};
this.loader = new Ext.tree.TreeLoader({
baseAttrs: {loaded: true} // Makes every ASyncNode think it's already loaded
});
MyTree.superclass.initComponent.call(this);
},
loadData : function(node, data) {
if(Ext.isString(data)) {
try {
data = Ext.decode(data);
}catch(e){
alert('error!'+e);
return;
}
}
// data is supposed to be an array now
try {
node.beginUpdate();
this.loadNode(node, data);
node.endUpdate();
}catch(e){
alert('error!'+e);
}
this.root.expand();
},
loadNode: function(node, data) {
for(var i = 0, len = data.length; i < len; i++) {
var d = data[i];
var n = this.loader.createNode(d);
if(n){
node.appendChild(n);
}
for(var p in d) {
if(Ext.isArray(d[p])) {
var subNode = this.loader.createNode({
text: p,
leaf: false
});
if(subNode){
n.appendChild(subNode);
}
this.loadNode(subNode, d[p]);
}
}
}
},
...
}
In your clientcode, you can now load the tree via:
MyTree.loadData(this.root,
[{id:123, text:'sub1', kids:[{id:11, text:'sub1-1'},{id:12, text:'sub1-2'}]}]
);
Bear in mind that when reloading nodes, you might need to delete them first...
Ronaldo
This is not a real extension, but more a tip: I needed a local tree that does not load data from the server, but is loaded from an array client side.
AFAIK, the TreePanel is configured to always go to the server to request data, which is not what I wanted.
Now the treeLoader, nor the ASyncTreeLoader have a mode='local' property, like the Store.
So here's my solution to use a 'mode=local' tree:
MyTree = Ext.extend(Ext.tree.TreePanel, {
layout : 'fit',
initComponent : function() {
this.root = {
id :'root',
text :'Root',
draggable :false,
type :'folder',
expand :true,
leaf: false,
loaded:true
};
this.loader = new Ext.tree.TreeLoader({
baseAttrs: {loaded: true} // Makes every ASyncNode think it's already loaded
});
MyTree.superclass.initComponent.call(this);
},
loadData : function(node, data) {
if(Ext.isString(data)) {
try {
data = Ext.decode(data);
}catch(e){
alert('error!'+e);
return;
}
}
// data is supposed to be an array now
try {
node.beginUpdate();
this.loadNode(node, data);
node.endUpdate();
}catch(e){
alert('error!'+e);
}
this.root.expand();
},
loadNode: function(node, data) {
for(var i = 0, len = data.length; i < len; i++) {
var d = data[i];
var n = this.loader.createNode(d);
if(n){
node.appendChild(n);
}
for(var p in d) {
if(Ext.isArray(d[p])) {
var subNode = this.loader.createNode({
text: p,
leaf: false
});
if(subNode){
n.appendChild(subNode);
}
this.loadNode(subNode, d[p]);
}
}
}
},
...
}
In your clientcode, you can now load the tree via:
MyTree.loadData(this.root,
[{id:123, text:'sub1', kids:[{id:11, text:'sub1-1'},{id:12, text:'sub1-2'}]}]
);
Bear in mind that when reloading nodes, you might need to delete them first...
Ronaldo