网上看到很多人不知道怎么在js代码的树上加右键菜单。我有实现的代码如下
但我就是不知道怎么灰掉不该出现的时候,如在非子节点时,不能用“增加到收藏夹”功能
Ext.ux.AddToFavoritesMenu = function(config) {
Ext.ux.AddToFavoritesMenu.superclass.constructor.call(this, config);
};
Ext.extend(Ext.ux.AddToFavoritesMenu, Ext.menu.Menu, {
tree : "",
items : [{
id : 'addToFavorite',
text : '增加到收藏夹',
enabled: this.leaf,
handler : function(item) {
var n = item.parentMenu.contextNode;
if(!n || !n.attributes.leaf){
msg("只能增加子节点到收藏夹!");
return;
}
var favoriteRpc = new Wsms.bs.Favorite();
var a = n.attributes;
var favorite = {};
favorite.path = n.getPath();
favorite.name = a.text;
favorite.href = a.href;
favorite.cls = a.cls;
favoriteRpc.addNew(favorite);
}
}, {
id : 'openUrl',
text : '打开',
handler : function(item) {
var n = item.parentMenu.contextNode;
n.fireEvent("click", n);
}
}]
});
MenuPanel = function() {
MenuPanel.superclass.constructor.call(this, {
id:'menu-tree',
split:true,
width: 280,
border :false,
minSize: 175,
maxSize: 500,
collapsible: true,
margins:'0 0 3 3',
cmargins:'0 0 0 0',
rootVisible:false,
// 节点之间连接的横竖线
lines:true,
autoScroll:true,
animCollapse:false,
animate: false,
collapseMode:'mini',
loader: new Ext.tree.TreeLoader({
preloadChildren: true,
clearOnLoad: true
}),
contextMenu : new Ext.ux.AddToFavoritesMenu(),
listeners : {
contextmenu : function(node, e) {
node.select();
var c = node.getOwnerTree().contextMenu;
c.leaf = node.attributes.leaf;
c.contextNode = node;
c.showAt(e.getXY());
}
},
root: new Ext.tree.AsyncTreeNode({
text:'根',
id:'root',
expanded:true,
children:Docs.classData
}),
collapseFirst:false
});
// no longer needed!
//new Ext.tree.TreeSorter(this, {folderSort:true,leafAttr:'isClass'});
this.getSelectionModel().on('beforeselect', function(sm, node){
return node.isLeaf();
});
};
Ext.extend(MenuPanel, Ext.tree.TreePanel, {
});