环境:Dojo 0.4
以下是核心代码,主要是是扩展了dojo.widget.TreeNodeV3,当然你也可以使用dojo.widget.TreeNode,方法一样,找到你的dojo源文件,重载一个合适的方法,将此方法copy过来进行重载方法的定义,以便动态创建节点时,根据条件设置右键菜单:
页面代码
- <%@ page language="java" pageEncoding="gbk"%>
- <html>
- <head>
- <title>dojo tree</title>
- <link href="${pageContext.request.contextPath}/css/css.css" rel="stylesheet" type="text/css">
-
- <script type="text/javascript" src="${pageContext.request.contextPath}/dojo/dojo.js"></script>
- <script language="JavaScript" type="text/javascript">
- dojo.require("dojo.widget.TreeV3");
- dojo.require("dojo.widget.TreeNodeV3");
- dojo.require("dojo.widget.TreeLoadingControllerV3");
- dojo.require("dojo.widget.TreeRpcControllerV3");
- dojo.require("dojo.widget.TreeSelectorV3");
- dojo.require("dojo.widget.TreeDocIconExtension");
- dojo.require("dojo.widget.TreeToggleOnSelect");
- dojo.require("dojo.widget.TreeExpandToNodeOnSelect");
- dojo.require("dojo.widget.Menu2");
- dojo.require("dojo.widget.Tooltip");
- dojo.require("dojo.widget.LayoutContainer");
- dojo.require("dojo.widget.ContentPane");
- dojo.require("dojo.widget.SplitContainer");
- dojo.require("dojo.io");
- dojo.require("dojo.string.common");
- dojo.require("dojo.widget.TreeContextMenuV3");
-
- //扩展dojo.widget.TreeNodeV3类
- dojo.widget.defineWidget(
- "dojo.widget.TreeNodeV3",
- dojo.widget.TreeNodeV3,
- function() {
- this.object = {};
- },
-
-
- {
- //====================
- //重载原有方法,获得相关属性,控制右键菜单项
- buildRendering: function(args, fragment, parent) {
- if (args.tree) {
- this.tree = dojo.lang.isString(args.tree) ? dojo.widget.manager.getWidgetById(args.tree) : args.tree;
- } else if (parent && parent.tree) {
- this.tree = parent.tree;
- }
- if (!this.tree) {
- dojo.raise("Can't evaluate tree from arguments or parent");
- }
- thisthis.domNode = this.tree.nodeTemplate.cloneNode(true);
- thisthis.expandNode = this.domNode.firstChild;
- thisthis.contentNode = this.domNode.childNodes[1];
- thisthis.labelNode = this.contentNode.firstChild;
- if (this.labelClass) {
- dojo.html.addClass(this.labelNode, this.labelClass);
- }
- if (this.contentClass) {
- dojo.html.addClass(this.contentNode, this.contentClass);
- }
- thisthis.domNode.widgetId = this.widgetId;
- thisthis.labelNode.innerHTML = this.title;
- //设置不可预览的节点
- if(this.isFolder || (this.nodeDocType && !(this.nodeDocType=='datasource' || this.nodeDocType=='sql'))){
- this.actionsDisabled = ['preview'];
- }
- }
- //======================
- }
- );
- dojo.hostenv.writeIncludes();
-
- //......others
- </script>
- <body>
- ......
- <div dojoType="TreeContextMenuV3" toggle="explode" contextMenuForWindow="false" widgetId="treeContextMenu">
- <div dojoType="TreeMenuItemV3" treeActions="addChild" iconSrc="../images/add.gif" caption="新建" widgetId="treeContextMenuCreate"></div>
- <div dojoType="TreeMenuItemV3" treeActions="edit" iconSrc="../images/edit.gif" caption="编辑" widgetId="treeContextMenuEdit"></div>
- <div dojoType="TreeMenuItemV3" treeActions="remove" iconSrc="../images/del.gif" caption="删除" widgetId="treeContextMenuRemove"></div>
- <div dojoType="TreeMenuItemV3" treeActions="preview" iconSrc="../images/preview.gif" caption="预览" widgetId="treeContextMenuPreview"></div>
- </div>
- ......
- </body>
- </html>