Context menu in ASP.NET Core Grid Component

27 Dec 202211 minutes to read

The Grid has options to show the context menu when right clicked on it. To enable this feature, you need to define either default or custom item in the contextMenuItems.

The default items are in the following table.

Items Description
AutoFit Auto fit the current column.
AutoFitAll Auto fit all columns.
Edit Edit the current record.
Delete Delete the current record.
Save Save the edited record.
Cancel Cancel the edited state.
Copy Copy the selected records.
PdfExport Export the grid data as Pdf document.
ExcelExport Export the grid data as Excel document.
CsvExport Export the grid data as CSV document.
Group Group the current column.
Ungroup Ungroup the current column.
SortAscending Sort the current column in ascending order.
SortDescending Sort the current column in descending order.
FirstPage Go to the first page.
PrevPage Go to the previous page.
LastPage Go to the last page.
NextPage Go to the next page.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource"  allowSorting="true" allowExcelExport="true" allowPdfExport="true"  contextMenuItems="@(new List<object>() { "AutoFit", "AutoFitAll", "SortAscending", "SortDescending","Copy", "Edit", "Delete", "Save", "Cancel","PdfExport", "ExcelExport", "CsvExport", "FirstPage", "PrevPage","LastPage", "NextPage"})" allowPaging="true">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings> 
    <e-grid-columns> 
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column> 
        <e-grid-column field="CustomerID" headerText="Customer Name"  width="150" ></e-grid-column> 
        <e-grid-column field="Freight" headerText="Freight" editType="numericedit" textAlign="Right" format="C2"  width="120"></e-grid-column> 
        <e-grid-column field="ShipName" headerText="Ship Name" width="200"></e-grid-column> 
        <e-grid-column field="ShipCountry" headerText="Ship Country" editType="dropdownedit" width="150"></e-grid-column> 
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column> 
    </e-grid-columns> 
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Custom context menu items

The custom context menu items can be added by defining the contextMenuItems as a collection of ContextMenuItemModel. Actions for this customized items can be defined in the contextMenuClick event.

@{ 
    List<object> ContextMenuitems = new List<object>();
    ContextMenuitems.Add(new { text = "Copy with headers", target= ".e-content", id = "copywithheader" });
 }

 <ejs-grid id="Grid" dataSource="@ViewBag.DataSource" contextMenuItems="ContextMenuitems" contextMenuClick="contextMenuClick" >     
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true"  textAlign="Left"  width="125"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Employee ID"   width="125" textAlign="Right" ></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="120"></e-grid-column>                 
        <e-grid-column field="ShipCity"  headerText="Ship City"   width="170"></e-grid-column>
        <e-grid-column field="CustomerID"  headerText="Customer ID" textAlign="Right"  width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
function contextMenuClick(args) {
    if (args.item.id === 'copywithheader') {
        this.copy(true);
    }
}
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

NOTE

You can hide or show an item in context menu for specific area inside of grid by defining the Target property.

Show context menu on left click

By default, the context menu items will be shown in the Grid using the right mouse click action. Show the context menu items during the left mouse click action using the created and context menu’s beforeOpen events of the Grid.

Using the onclick eventlistener of Grid , you can get the clicked position values and send them to the open method of the context menu in the onclick event of the Grid. Also, we have prevented the default right click action to open the context menu items using the created event of the Grid.

This is demonstrated in the following sample.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" created="created" contextMenuItems="@(new List<object>() { "Copy", "Edit", "Delete"})" allowPaging="true">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" editType="numericedit" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="200"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" editType="dropdownedit" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function created() {
        var grid = document.getElementById("Grid").ej2_instances[0];
        grid.contextMenuModule.contextMenu.beforeOpen = (args) => {
            if (args.event && args.event.which === 3) args.cancel = true;
            args.event = values;
            grid.contextMenuModule.contextMenuBeforeOpen(args);
        };
    }
    document.getElementById('Grid').onclick = (event) => {
        values = event;
        var grid = document.getElementById("Grid").ej2_instances[0];
        grid.contextMenuModule.contextMenu.open(
            values.pageY + pageYOffset,
            values.pageX + pageXOffset
        );
    };
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Enable or disable context menu items

It is possible to enable or disable the default and custom context menu items in the Grid component. This is achieved by using the enableItems method of the ContextMenu. To enable or disable menu items, set the enable parameter in the enableItems method to true, and vice versa.

In the following sample, the Copy item is enabled or disabled based on some condition (as per the needs of the application) in the RowSelected event of the Grid.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" contextMenuItems="@(new List<object>() {"Copy", "Edit", "Delete"})" allowPaging="true" rowSelected="rowSelected">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" editType="numericedit" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="200"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" editType="dropdownedit" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function rowSelected(args) {
        var grid = document.getElementById("Grid").ej2_instances[0];
        var contextMenuObj = grid.contextMenuModule.contextMenu;
        if (args.data.OrderID % 2 === 0) {
            contextMenuObj.enableItems(['Copy'], false);
        } else {
            contextMenuObj.enableItems(['Copy'], true);
        }
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}