mknopp
10 Mar 2008, 8:35 PM
I am working on setting up a context menu option for a tree.
mabello, was nice enough to provide me with code to achieve this using a specialized treeloader and listeners.
I tried to streamline this code by removing the modified treeloader and passing a listener attribute from PHP through JSON. However, it does not appear to be adding the listener attribute to the nodes.
Can a listener be passed into a standard treeloader through JSON? If so, what am I doing wrong? If not then is my only option the specialized treeloader?
Thank you for any help.
javascript
xt.onReady(function(){
Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
// Right Click Code
var saveAction = new Ext.Action({
text: "Save",
handler: function(){
alert("Saving node of id->" + saveAction.selectedNode.attributes.id);
}
});
var onContextHide = function(scope){
if(this.ctxNode)
{
this.ctxNode.ui.removeClass('x-node-ctx');
this.ctxNode = null;
}
}
var userNodeInterceptor = {
click: function(node, eventObject){
var nodeId = node.attributes.id;
alert("Click on node of id->" + nodeId);
},
contextmenu : function(node, e){
if(!this.menu) // create context menu on first right click
{
this.menu = new Ext.menu.Menu({
id:'feeds-ctx',
items: [saveAction]
});
this.menu.on('hide', onContextHide, this);
}
if(this.ctxNode)
{
this.ctxNode.ui.removeClass('x-node-ctx');
this.ctxNode = null;
}
//if(node.isLeaf()){
this.ctxNode = node;
this.ctxNode.ui.addClass('x-node-ctx');
this.ctxNode.select();//Select the node
saveAction.selectedNode = node;
this.menu.showAt(e.getXY());
//}
}
};
// tabs for the center
var tabs = new Ext.TabPanel({
region: 'center',
margins:'3 3 3 0',
activeTab: 0,
defaults:{autoScroll:true},
items:[{
title: 'Contact',
html: 'Text in Tab 1'
},{
title: 'Employer',
html: 'Text in Tab 2',
closable: true
},{
title: 'Vendor',
html: 'Text in Tab 3',
closable: true
}]
});
var tree_loader = new Ext.tree.TreeLoader({
dataUrl: 'ext_test.php',
preloadChildren: true
});
// Panel for the west
var nav = new Ext.tree.TreePanel({
title: 'Navigation',
region: 'west',
split: true,
width: 250,
collapsible: true,
margins:'3 0 3 3',
cmargins:'3 3 3 3',
useArrows: true,
autoScroll: true,
animate: true,
enableDD: false,
containerScroll: true,
rootVisible: false,
loader: tree_loader
});
var root = new Ext.tree.AsyncTreeNode({
text: 'NComp Data',
draggable: false,
id: 'source'
});
nav.setRootNode(root);
var win = new Ext.Window({
title: 'Layout Window',
renderTo: 'content',
resizable: false,
x: 10,
y: 10,
constrain: true,
draggable: false,
closable:false ,
width:800,
height:350,
//border:false,
plain:true,
layout: 'border',
items: [nav, tabs]
});
win.show();
root.expand();
});
Here is a sample of the JSON being passed back from the PHP.
[{"text":"Ray Abad","id":"contacts\/11","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Kibbles Bits","id":"contacts\/9","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Johnny Bravo","id":"contacts\/18","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Mike Bridge","id":"contacts\/13","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Jane Brown","id":"contacts\/2","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Jim Brown","id":"contacts\/19","cls":"file","leaf":"true","listeners":"userNodeInterceptor"}]
mabello, was nice enough to provide me with code to achieve this using a specialized treeloader and listeners.
I tried to streamline this code by removing the modified treeloader and passing a listener attribute from PHP through JSON. However, it does not appear to be adding the listener attribute to the nodes.
Can a listener be passed into a standard treeloader through JSON? If so, what am I doing wrong? If not then is my only option the specialized treeloader?
Thank you for any help.
javascript
xt.onReady(function(){
Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
// Right Click Code
var saveAction = new Ext.Action({
text: "Save",
handler: function(){
alert("Saving node of id->" + saveAction.selectedNode.attributes.id);
}
});
var onContextHide = function(scope){
if(this.ctxNode)
{
this.ctxNode.ui.removeClass('x-node-ctx');
this.ctxNode = null;
}
}
var userNodeInterceptor = {
click: function(node, eventObject){
var nodeId = node.attributes.id;
alert("Click on node of id->" + nodeId);
},
contextmenu : function(node, e){
if(!this.menu) // create context menu on first right click
{
this.menu = new Ext.menu.Menu({
id:'feeds-ctx',
items: [saveAction]
});
this.menu.on('hide', onContextHide, this);
}
if(this.ctxNode)
{
this.ctxNode.ui.removeClass('x-node-ctx');
this.ctxNode = null;
}
//if(node.isLeaf()){
this.ctxNode = node;
this.ctxNode.ui.addClass('x-node-ctx');
this.ctxNode.select();//Select the node
saveAction.selectedNode = node;
this.menu.showAt(e.getXY());
//}
}
};
// tabs for the center
var tabs = new Ext.TabPanel({
region: 'center',
margins:'3 3 3 0',
activeTab: 0,
defaults:{autoScroll:true},
items:[{
title: 'Contact',
html: 'Text in Tab 1'
},{
title: 'Employer',
html: 'Text in Tab 2',
closable: true
},{
title: 'Vendor',
html: 'Text in Tab 3',
closable: true
}]
});
var tree_loader = new Ext.tree.TreeLoader({
dataUrl: 'ext_test.php',
preloadChildren: true
});
// Panel for the west
var nav = new Ext.tree.TreePanel({
title: 'Navigation',
region: 'west',
split: true,
width: 250,
collapsible: true,
margins:'3 0 3 3',
cmargins:'3 3 3 3',
useArrows: true,
autoScroll: true,
animate: true,
enableDD: false,
containerScroll: true,
rootVisible: false,
loader: tree_loader
});
var root = new Ext.tree.AsyncTreeNode({
text: 'NComp Data',
draggable: false,
id: 'source'
});
nav.setRootNode(root);
var win = new Ext.Window({
title: 'Layout Window',
renderTo: 'content',
resizable: false,
x: 10,
y: 10,
constrain: true,
draggable: false,
closable:false ,
width:800,
height:350,
//border:false,
plain:true,
layout: 'border',
items: [nav, tabs]
});
win.show();
root.expand();
});
Here is a sample of the JSON being passed back from the PHP.
[{"text":"Ray Abad","id":"contacts\/11","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Kibbles Bits","id":"contacts\/9","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Johnny Bravo","id":"contacts\/18","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Mike Bridge","id":"contacts\/13","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Jane Brown","id":"contacts\/2","cls":"file","leaf":"true","listeners":"userNodeInterceptor"},{"text":"Jim Brown","id":"contacts\/19","cls":"file","leaf":"true","listeners":"userNodeInterceptor"}]