Search results

Template and Multilevel nesting

Template

The ContextMenu items can be customized by using the Render event. The item render event triggers while rendering each menu item. The event argument will be used to identify the menu item and customize it based on the requirement. In the following sample, the menu item is rendered with keycode for specified action in ContextMenu using the template. Here, the keycode is specified for Save as, View page source, and Inspect in the right side corner of the menu items by adding span element in the beforeItemRender event.

razor
template.cs
@section ControlsSection{

    <div id="contextmenutarget">Right click/Touch hold to open the ContextMenu </div>
    @Html.EJS().ContextMenu("contextmenu").Target("#contextmenutarget").Items((IEnumerable<object>)ViewBag.items).BeforeItemRender("beforeItemRender").Render()

}

<script>

    function beforeItemRender(args) {
        var shortCutSpan = createElement('span');
            var text = args.item.text;
            var shortCutText = text === 'Save as...' ? 'Ctrl + S' : (text === 'View page source' ?
            'Ctrl + U'      : 'Ctrl + Shift + I');
            shortCutSpan.textContent = shortCutText;
            args.element.appendChild(shortCutSpan);
            shortCutSpan.setAttribute('class','shortcut');
    }

</script>

<style>

#contextmenutarget {
    border: 1px dashed;
    height: 250px;
    padding: 10px;
    position: relative;
    text-align: center;
    color: gray;
    line-height: 17;
    font-size: 14px;
}

</style>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    public class ContextMenuController : Controller
    {
        public IActionResult Template()
        {
            List<object> menuItems = new List<object>();
            menuItems.Add(new
            {
                text = "Save as..."
            });
            menuItems.Add(new
            {
                text = "View page source"
            });
            menuItems.Add(new
            {
                text = "Inspect"
            });
            ViewBag.menuItems = menuItems;
            return View();
        }
    }
}

Multilevel nesting

The Multiple level nesting supports in ContextMenu. It can be achieved by mapping the items property inside the parent menuItems. In the below sample, three level nesting of ContextMenu is provided.

razor
nesting.cs
@section ControlsSection{

    <div id="contextmenutarget">Right click/Touch hold to open the ContextMenu </div>
    @Html.EJS().ContextMenu("contextmenu").Target("#contextmenutarget").Items((IEnumerable<object>)ViewBag.items).Render()

}

<style>

#contextmenutarget {
    border: 1px dashed;
    height: 250px;
    padding: 10px;
    position: relative;
    text-align: center;
    color: gray;
    line-height: 17;
    font-size: 14px;
}

</style>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    public class ContextMenuController : Controller
    {
        public IActionResult Nesting()
        {
            List<object> menuItems = new List<object>();
            menuItems.Add(new
            {
                text = "Show All Bookmarks"
            });
            menuItems.Add(new
            {
                text = "Bookmarks Toolbar",
                items = new List<object>()
                {
                    new { 
                        text = "Most Visited",
                        items = new List<object>()
                        {
                            new {
                                text = "Google"
                            },
                            new {
                                text = "Gmail"
                            }
                        }
                    },
                    new {
                        text = "Recently Added"
                    }
                }
            });
            ViewBag.menuItems = menuItems;
            return View();
        }
    }
}

See Also