Context menu in Vue Grid component

28 Mar 202316 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.

To use the context menu, inject the ContextMenu module in the provide section.

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.
<template>
    <div id="app">
        <ejs-grid :dataSource='data' id="gridcomp" :allowPaging='true' :allowExcelExport='true' :allowPdfExport='true' height='215px' :allowSorting='true'
        :contextMenuItems="contextMenuItems" :editSettings='editing' :allowGrouping='true'>
        <e-columns>
            <e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right" isPrimaryKey='true'></e-column>
            <e-column field='CustomerID' headerText='Customer Name'></e-column>
            <e-column field='Freight' headerText='Freight' format='C2' textAlign="Right" editType='numericedit'></e-column>
            <e-column field='ShipCity' headerText='Ship City' width='150'></e-column>
        </e-columns>
    </ejs-grid>
    </div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, ContextMenu, Page, Resize, Sort, Group, Edit, PdfExport, ExcelExport  } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';

Vue.use(GridPlugin);

export default {
  data() {
    return {
      data: data,
      contextMenuItems: ['AutoFit', 'AutoFitAll', 'SortAscending', 'SortDescending',
                'Copy', 'Edit', 'Delete', 'Save', 'Cancel',
                'PdfExport', 'ExcelExport', 'CsvExport', 'FirstPage', 'PrevPage',
                'LastPage', 'NextPage', 'Group', 'Ungroup'],
      editing: {allowEditing: true, allowDeleting: true}
    };
  },
  provide : {
      grid: [ContextMenu, Page, Resize, Sort, Group, Edit, PdfExport, ExcelExport]
  }
}
</script>
<style>
 @import "https://ej2.syncfusion.com/vue/documentation/node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>

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.

<template>
    <div id="app">
        <ejs-grid ref='grid' id='gridcomp' :dataSource='data' :allowSelection='true' :allowPaging='true' height='265px' :contextMenuItems='contextMenuItems' :contextMenuClick='contextMenuClick'>
            <e-columns>
                <e-column field='EmployeeID' :isPrimaryKey='true' headerText='Employee ID' textAlign='Right' width=120></e-column>
                <e-column field='FirstName' headerText='FirstName' width=150></e-column>
                <e-column field='LastName' headerText='Last Name' width=150></e-column>
                <e-column field='City' headerText='City' width=150></e-column>
            </e-columns>
        </ejs-grid>
    </div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, ContextMenu, Page } from "@syncfusion/ej2-vue-grids";
import { employeeData } from './datasource.js';

Vue.use(GridPlugin);

export default {
  data() {
    return {
      data: employeeData,
      contextMenuItems: [{text: 'Copy with headers', target: '.e-content', id: 'copywithheader'}]
    };
  },
  methods: {
      contextMenuClick: function(args) {
        if(args.item.id === 'copywithheader') {
            this.$refs.grid.copy(true);
        }
    }
  },
  provide: {
      grid: [ContextMenu, Page]
  }
}
</script>
<style>
 @import "https://ej2.syncfusion.com/vue/documentation/node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>

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.

<template>
    <div id="app">
        <ejs-grid ref='grid' :dataSource='data' :allowPaging='true'
        height='265px'
        :contextMenuItems='contextMenuItems'
        :editSettings  ='editOptions'
        :created = 'created' v-on:click.native="clicked">
            <e-columns>
                <e-column field='EmployeeID' :isPrimaryKey='true' headerText='Employee ID'
                textAlign='Right' width=120></e-column>
                <e-column field='FirstName' headerText='FirstName' width=150></e-column>
                <e-column field='LastName' headerText='Last Name' width=150></e-column>
                <e-column field='City' headerText='City' width=150></e-column>
            </e-columns>
        </ejs-grid>
    </div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, ContextMenu, Page, Edit } from "@syncfusion/ej2-vue-grids";
import { employeeData } from './datasource.js';

Vue.use(GridPlugin);

export default {
  data() {
    return {
      values: "",
      data: employeeData,
      contextMenuItems: ['Copy', 'Edit', 'Delete', 'Save', 'Cancel'],
      editOptions: {
        allowDeleting: true,
        allowEditing: true,
        allowAdding: true,
      },
    };
  },
  methods: {
    created:function(args) {
      this.$refs.grid.ej2Instances.contextMenuModule.contextMenu.beforeOpen = (
        args
      ) => {
        if (args.event && args.event.which === 3) args.cancel = true;
        args.event = this.values;
        this.$refs.grid.ej2Instances.contextMenuModule.contextMenuBeforeOpen(
          args
        );
      };
    },
    clicked: function(event) {
      this.values = event;
      this.$refs.grid.ej2Instances.contextMenuModule.contextMenu.open(
        this.values.pageY + pageYOffset,
        this.values.pageX + pageXOffset
      );
    }
  },
  provide: {
    grid: [ContextMenu, Page, Edit]
  }
}
</script>
<style>
 @import "https://ej2.syncfusion.com/vue/documentation/node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>

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

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.

<template>
    <div id="app">
        <ejs-grid  ref='grid' :dataSource='data' id="gridcomp" :allowPaging='true' height='215px'
        :contextMenuItems="contextMenuItems" :editSettings='editSettings' :rowSelected='rowSelected'>
        <e-columns>
            <e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right" isPrimaryKey='true'></e-column>
            <e-column field='CustomerID' headerText='Customer Name'></e-column>
            <e-column field='Freight' headerText='Freight' format='C2' textAlign="Right" editType='numericedit'></e-column>
            <e-column field='ShipCity' headerText='Ship City' width='150'></e-column>
        </e-columns>
    </ejs-grid>
    </div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, ContextMenu, Page, Edit } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';

Vue.use(GridPlugin);

export default {
  data() {
    return {
      data: data,
      contextMenuItems: ['Copy', 'Edit', 'Delete'],
      editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true }
    };
  },
  methods: {
     rowSelected: function (args) {
      if (args.data.OrderID % 2 === 0) {
           this.$refs.grid.ej2Instances.contextMenuModule.contextMenu.enableItems(['Copy'], false);
       } else {
           this.$refs.grid.ej2Instances.contextMenuModule.contextMenu.enableItems(['Copy'], true);
      }
     }
  },
  provide : {
      grid: [ContextMenu, Page, Edit]
  }
}
</script>
<style>
 @import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>