Contents
- Render UL and LI template
- Show table in sub ContextMenu
- Show UI components in ContextMenu
Having trouble getting help?
Contact Support
Contact Support
Template in EJ2 JavaScript Context menu control
2 May 202314 minutes to read
Render UL and LI template
Add the HTML UL tag with id
attribute as #contextmenu
in your index.html
file with required LI tags and also add target element on which the ContextMenu has to be opened.
ej.base.enableRipple(true);
var menuObj = new ej.navigations.ContextMenu({ target: '#target' }, '#contextmenu');
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Essential JS 2">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<!--style reference from app-->
<link href="styles.css" rel="stylesheet">
<!--system js reference and configuration-->
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<!--target element-->
<div id="target">Right click / Touch hold to open the ContextMenu</div>
<!--element which is going to render-->
<ul id="contextmenu" class="list">
<li>Cut</li>
<li>Copy</li>
<li>Paste</li>
</ul>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Show table in sub ContextMenu
Menu items of the ContextMenu can be customized according to the requirement. The section explains about how to customize table template
in sub menu item.
This can be achieved by appending table layout while li
rendering by using beforeItemRender
event.
ej.base.enableRipple(true);
var header = document.createElement('h4');
header.textContent = 'Insert Table';
var table = document.createElement('table');
for (var i = 0; i < 5; i++) {
var row = document.createElement('tr');
table.appendChild(row);
for (var j = 0; j < 6; j++) {
var col = document.createElement('td');
row.appendChild(col);
col.setAttribute('class', 'e-border');
}
}
var menuItems = [
{
text: 'Cut',
iconCss: 'e-cm-icons e-cut'
},
{
text: 'Copy',
iconCss: 'e-icons e-copy'
},
{
text: 'Paste',
iconCss: 'e-cm-icons e-paste'
},
{
separator: true
},
{
text: 'Link',
iconCss: 'e-icons e-link'
},
{
text: 'Table',
iconCss: 'e-icons e-table',
items: [
{
id: 'table'
}
]
}];
var menuOptions = {
target: '#target',
items: menuItems,
beforeItemRender: function(args) {
if (args.item.id === 'table') {
args.element.classList.add('bg-transparent');
args.element.appendChild(header);
args.element.appendChild(table);
}
}
};
var menuObj = new ej.navigations.ContextMenu(menuOptions, '#contextmenu');
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Essential JS 2">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<!--style reference from app-->
<link href="styles.css" rel="stylesheet">
<!--system js reference and configuration-->
<style>
.e-contextmenu-wrapper ul .e-menu-item.bg-transparent {
background-color: transparent;
line-height: normal;
height: auto;
}
h4 {
text-align: center;
margin-top: 5px;
margin-bottom: 5px;
}
</style>
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="target">Right click / Touch hold to open the ContextMenu</div>
<ul id="contextmenu"></ul>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Show UI components in ContextMenu
UI components can also be placed inside the each li
element of ContextMenu.
In the following example, CheckBox component is placed inside each li
element and this can be achieved by creating CheckBox component in beforeItemRender
event and appending it into the li
element.
ej.base.enableRipple(true);
var menuItems = [
{ text: 'Option 1' },
{ text: 'Option 2' },
{ text: 'Option 3' }
];
var i = 1;
var menuOptions = {
target: '#target',
items: menuItems,
beforeItemRender: function(args) {
var checkbox = new ej.buttons.CheckBox({ label: 'Option'+ i, checked: i%2 === 0 ? true : false });
args.element.innerHTML = '';
checkbox.appendTo('#checkbox'+i);
var checkbox = document.getElementsByClassName('e-checkbox-wrapper');
args.element.appendChild(checkbox[0]);
i++;
},
beforeClose: function(args) {
args.cancel = true;
},
select: function(args) {
var selectedElem = args.element.parentElement.querySelectorAll('.e-selected');
for(var i=0; i < selectedElem.length; i++){
var ele = selectedElem[i];
ele.classList.remove('e-selected');
}
var checkbox = args.element.childNodes[0];
var frame = checkbox.querySelector('.e-frame');
if (checkbox && frame.classList.contains('e-check')) {
frame.classList.remove('e-check');
} else if (checkbox) {
frame.classList.add('e-check');
}
}
};
var menuObj = new ej.navigations.ContextMenu(menuOptions, '#contextmenu');
<!DOCTYPE html><html lang="en"><head>
<title>Essential JS 2</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Essential JS 2">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<!--style reference from app-->
<link href="styles.css" rel="stylesheet">
<!--system js reference and configuration-->
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="target">Right click / Touch hold to open the ContextMenu</div>
<ul id="contextmenu"></ul>
<input type="checkbox" id="checkbox1" style="display:none">
<input type="checkbox" id="checkbox2" style="display:none">
<input type="checkbox" id="checkbox3" style="display:none">
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>