How would I go about adding and removing ContextMenuItems from an existing ContextMenu with client script?
Add/remove ContextMenuItem ClientSide
Rob,
In current version, it's not supported to add or remove ContextMenuItems from an existing ContextMenu on client part, we will add it to our tracking system to see if we will support it in the future release, thanks for pointing it out. Now there is a trick though which allows you to add/remove the ContextMenuItems when map is created, please have a look at the attached code for detail.
Thanks,
Ben
640-Add_or_Remove_ContextMenuItemClient.txt (624 Bytes)
That worked great, thanks. I found that it works at any time (not just when map is created). Here is some simple wrappers to make things easier.
var olMap;
// add new item after first item
var contextMenuItem = {
click: false,
css: ‘’,
hoverCss: ‘’,
html: “[script removed]”,
id: ‘testContextMenuItem’
};
InsertContextMenuItem(1, contextMenuItem);
// remove item at index 1
RemoveContextMenuItem(1);
function OnMapCreated(map){
olMap = map;
}
function InsertContextMenuItem(index, newContextMenuItem)
{
olMap.contextMenu.items.splice(index, 0, newContextMenuItem);
}
function RemoveContextMenuItem(index)
{
olMap.contextMenu.items.splice(index, 1);
}
Thanks for sharing! Rob