)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Essential JS2

Syncfusion jQuery based widgets are no longer in active development. Switch to our pure JavaScript based next generation Essential JS 2 library.

Syncfusion Feedback

how to show context menu on left click?

Platform: jQuery |
Control: ejGrid |
Published Date: July 6, 2018 |
Last Revised Date: February 26, 2020

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

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon