Hi,
I have problem to make the context menus work. Below is the code. I manage to get the treeview and the context menu but it seems the context menus are not working. Could you please help me to solve it.
I have tried many ways to solve this problem but it still not working.
Thank you.
<?xml version="1.0" encoding="UTF-8" ?>
Tree Test Page
var djConfig = { isDebug: true };
/* Load Dojo engine */
dojo.require("dojo.lang.*");
dojo.require("dojo.widget.*");
dojo.require("dojo.widget.Tree");
dojo.require("dojo.widget.TreeNode");
dojo.require("dojo.widget.TreeContextMenu");
dojo.require("dojo.widget.TreeBasicController");
//this code working
var treeMenuItem = "TreeMenuItem";
function addTreeContextMenu(){
var djWdgt = dojo.widget;
var ctxMenu = djWdgt.createWidget("TreeContextMenu",{widgetId:"treeCtxMenu"});
ctxMenu.addChild(djWdgt.createWidget(
treeMenuItem,{caption:"Add Item"}));
ctxMenu.addChild(djWdgt.createWidget(
treeMenuItem,{caption:"Edit Item"}));
document.body.appendChild(ctxMenu.domNode);
var myTree = dojo.widget.manager.getWidgetById("myTree");
/* Bind the context menu to the tree */
ctxMenu.listenTree(myTree);
}
dojo.addOnLoad( function(){
addTreeContextMenu();
});
//this code is not working
var DemoTreeManager = {
djWdgt: null,
myTreeWidget: null,
addTreeContextMenu: function(){
var ctxMenu = this.djWdgt.createWidget("TreeContextMenu",{widgetId:"treeCtxMenu"});
ctxMenu.addChild(this.djWdgt.createWidget(
"TreeMenuItem",{caption:"Add Child Menu Item",
widgetId:"ctxAdd"}));
ctxMenu.addChild(this.djWdgt.createWidget(
"TreeMenuItem",{caption:"Delete this Menu Item",
widgetId:"ctxDelete"`}));
document.body.appendChild(ctxMenu.domNode);
/* Bind the context menu to the tree */
var myTree = dojo.widget.manager.getWidgetById("myTree");
ctxMenu.listenTree(myTree);
},
addController: function(){
this.djWdgt.createWidget(
"TreeBasicController",
{widgetId:"myTreeController",DNDController:"create"}
);
},
bindEvents: function(){
/* Bind the functions in the TreeActions object to the
context menu entries */
dojo.event.topic.subscribe("ctxAdd/engage",
function (menuItem) { TreeActions.addNewNode(menuItem.getTreeNode(),
"myTreeController"); }
);
dojo.event.topic.subscribe("ctxDelete/engage",
function (menuItem) { TreeActions.removeNode(menuItem.getTreeNode(),
"myTreeController"); }
);
},
init: function(){
/* Initialize this object */
this.djWdgt = dojo.widget;
this.myTreeWidget = this.djWdgt.manager.
getWidgetById("myTreeWidget");
this.addTreeContextMenu();
this.addController();
this.bindEvents();
}
};
var TreeActions = {
addNewNode: function(parent,controllerId){
this.controller = dojo.widget.manager.getWidgetById(controllerId);
if (!parent.isFolder) {
parent.setFolder();
}
var res = this.controller.createChild(parent, 0, { title: "New node" });
},
removeNode: function(node,controllerId){
if (!node) {
alert("Nothing selected to delete",);
return false;
}
this.controller = dojo.widget.manager.getWidgetById(controllerId);
var res = this.controller.removeNode(node, dojo.lang.hitch(this));
}
};
dojo.addOnLoad(function(){
DemoTreeManager.init()
});
News
A few fixes...
1. The line widgetId:"ctxDelete"`})); contains wrong character - `.
2. The line alert("Nothing selected to delete",); contains syntax error - , should be deleted.
3. I cannot run your code with Dojo release 1.0.0 because it does not contain TreeContextMenu so includes fail. Just downloaded the latest version from http://dojotoolkit.org/downloads. Let me know if I need to download some other version...
Sincerely,
Alex
Context Menu on Treenode
Hi Alex,
Thanks for helping me to solve this problem. It was only the small error which I could not see. I managed to solve it. However, the node can be added but we could not edit the name. Do you have any ideas or recommended code to edit it?
I grab this code from : http://willcode4beer.com/ware.jsp?set=dojoTreeWidget.
Thanks,
Anna