Articles in this section
Category / Section

how to show context menu on left click?

3 mins read

This explains that how to display the context menu on a left click.

Solution:

We have achieved this using load event of ejGrid. In load event we have bind the click event to Grid and in this event, we have used showContextMenu method of ejMenu to display the context menu on Grid.

To display the context menu on pager we have bind the click event directly in pageSettings API using load event of ejGrid.

The following code example demonstrates how to show the context menu on a left click.

JS:

1. Render the Grid Control.

<div id="Grid"></div>
 
  <script type="text/javascript">
        $(function () {
            $("#Grid").ejGrid({
                // the datasource "window.gridData" is referred from jsondata.min.js
                dataSource: window.gridData,
                allowPaging: true,
                allowGrouping: true,
                pageSettings: { pageCount: 5 },
                load: "onLoad",
                editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
                toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] },
                contextMenuSettings: { enableContextMenu: true } ,
                columns: [
                        { field: "OrderID", isPrimaryKey: true, headerText: 'Order ID', textAlign: ej.TextAlign.Right, width: 90 },
                        { field: "CustomerID", headerText: 'Customer ID', width: 90 },
                        { field: "EmployeeID", headerText: 'Employee ID', textAlign: ej.TextAlign.Right, width: 90 },
                        { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, width: 80, format: "{0:C}" },
                        { field: "ShipName", headerText: 'Ship Name', width: 90 },
                        { field: "ShipCountry", headerText: 'Ship Country', width: 90 }                ]
            });
        });    
</script>

 

MVC:

@(Html.EJ().Grid<object>("Grid")
     .Datasource((IEnumerable<object>)ViewBag.datasource)
     .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
     .ToolbarSettings(toolbar =>
      {
        toolbar.ShowToolbar().ToolbarItems(items =>
        {
          items.AddTool(ToolBarItems.Add);
          items.AddTool(ToolBarItems.Edit);
          items.AddTool(ToolBarItems.Delete);
          items.AddTool(ToolBarItems.Update);
          items.AddTool(ToolBarItems.Cancel);
       });
     })
     .ContextMenuSettings(contextMenu => { contextMenu.EnableContextMenu(); })
     .AllowPaging()
     .AllowGrouping()
     .PageSettings(page => page.PageCount(5))
     .ClientSideEvents(eve => eve.Load("onLoad"))
     .Columns(col =>
      {
        col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(90).Add();
        col.Field("CustomerID").HeaderText("Customer ID").Width(90).Add();
        col.Field("EmployeeID").HeaderText("Employee ID").Width(90).TextAlign(TextAlign.Right).Add();
        col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Format("{0:C}").Width(80).Add();
        col.Field("ShipName").HeaderText("Ship Name").Width(90).Add();
        col.Field("ShipCountry").HeaderText("Ship Country").Width(90).Add();
      })
 )

 

ASP.NET:

<ej:Grid ID="Grid" runat="server" AllowPaging="True" AllowGrouping="true">            
            <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings>
            <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
            <ContextMenuSettings EnableContextMenu="true"></ContextMenuSettings>
            <ClientSideEvents Load="onLoad" />
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True" TextAlign="Right" Width="90"></ej:Column>
                <ej:Column Field="CustomerID" HeaderText="Customer ID" TextAlign="Right" Width="90"></ej:Column>
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="75" />
                <ej:Column Field="Freight" HeaderText="Freight" Width="80" TextAlign="Right" Format="{0:C}" />
                <ej:Column Field="ShipName" HeaderText="Ship Name" Width="90"></ej:Column>
                <ej:Column Field="ShipCountry" HeaderText="Ship Country" Width="90"></ej:Column>
            </Columns>
</ej:Grid>

 

ASP.NET CORE:

 
<ej-grid id="FlatGrid" allow-paging="true" allow-grouping="true" datasource="ViewBag.DataSource" load="onLoad">
    <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true"></e-edit-settings>
    <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> {"add","edit","delete","update","cancel"}' />
    <e-context-menu-settings enable-context-menu="true"></e-context-menu-settings>
    <e-columns>
        <e-column field="OrderID" is-primary-key="true" header-text="Order ID" text-align="Right" width="90"></e-column>
        <e-column field="CustomerID" header-text="CustomerID" text-align="Right" width="90" ></e-column>
        <e-column field="EmployeeID" header-text="Employee ID" text-align="Right" width="90" ></e-column>
        <e-column field="Freight" format="{0:C}" header-text="Freight" text-align="Right" width="80"></e-column>
        <e-column field="ShipName" header-text="Ship Name" width="90"></e-column>
        <e-column field="ShipCountry" header-text="Ship Country" width="90"></e-column>
    </e-columns>
</ej-grid>
 

 

Angular:

 
<ej-grid #grid (load)="onLoad($event)" [dataSource]="gridData" [allowPaging]="true" [pageSettings]="pageSettings" [allowGrouping]="true" [editSettings.allowAdding]='true'  [editSettings.allowEditing]='true' [editSettings.allowDeleting]='true' [toolbarSettings.showToolbar]="true" [toolbarSettings.toolbarItems]="tools"  [contextMenuSettings.enableContextMenu]="true">
    <e-columns>
        <e-column field="OrderID" headerText="OrderID" [isPrimaryKey]="true" textAlign="right" width="90"></e-column>
        <e-column field="CustomerID" headerText="CustomerID" textAlign="right" width="90"></e-column>
        <e-column field="EmployeeID" headerText="EmployeeID" textAlign="right" width="75"></e-column>
        <e-column field="Freight" headerText="Freight" textAlign="right" width="80" format="{0:C}" ></e-column>
        <e-column field="ShipName" headerText="Ship Name" width="90"></e-column>
        <e-column field="ShipCountry" headerText="Ship Country" width="90"></e-column>
    </e-columns>
 </ej-grid>
 

 

TS File:

2. In AngularJS we bind the click event directly to the pageSettings API for show the context menu in pager.

@ViewChild('grid') GridModel: EJComponents<any, any>;
 
    export class GridDetailTempComponent {
 
      public gridData;
      public pageSettings;
        
      public tools;
 
      constructor() {
       
        this.tools = ["add", "edit", "delete", "cancel", "update"];
 
        this.gridData = (window as any).gridData;
 
        this.pageSettings = {
            pageSize: 5, click: function (e) {
                var obj = $("#Grid_Context").ejMenu("instance");
                var target = e.event.target;
                obj.showContextMenu(null, null, target, e.event, true)
            }
        };
 
        }  
    }
 
    onLoad(event){
 
       $("#Grid").on("click",function(e)
            {
                var obj = $("#Grid_Context").ejMenu("instance"); 
                var target =e.target;
                var index = $(e.target).index();
                var gridobj = $(".e-grid").ejGrid("instance");
                var field = gridobj.getColumnByIndex(index).field;
                var flag = $(e.target).hasClass("e-headercelldiv");
                if (flag) {  
                    obj.showContextMenu(null, null, target, e, true) 
                } 
                else
                    obj.showContextMenu(null, null, target, e, true)  
                })
        }
}

 

JS:

3. In load event we have bind click event to the Grid element. In this click event we have taken ejMenu instance and we also able to get the target in the arguments of click event. Based on the target element we have display the appropriate context menu for the Grid header, content and Footer.

 
<script type="text/javascript">
    var GridID;
 
    function onLoad(args) {
        GridID = this.element.attr("id");
        $("#" + GridID).on("click", function (e) {
            var obj = $("#" + GridID + "_Context").ejMenu("instance");
            var target = e.target;
            var index = $(e.target).index();
            var gridobj = $(".e-grid").ejGrid("instance");
            var field = gridobj.getColumnByIndex(index).field;
            var flag = $(e.target).hasClass("e-headercelldiv");
            if (flag) {  
               obj.showContextMenu(null, null, target, e, true) 
            } 
            else
               obj.showContextMenu(null, null, target, e, true)  
            })
 
            this.model.pageSettings.click = function (e) {
                 var obj = $("#Grid_Context").ejMenu("instance");
                 var target = e.event.target;
                 obj.showContextMenu(null, null, target, e.event, true)
            }
    }
</script>

 

 

Result:

 

Grid with Context

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied