Column Menu

8 Mar 202210 minutes to read

The column menu has options to integrate features like sorting, grouping, filtering, column chooser, and autofit. It will show a menu with the integrated feature when users click on multiple icon of the column. To enable column menu, you need to define the showColumnMenu property as true.

The default items are displayed in following table.

Item Description
SortAscending Sort the current column in ascending order.
SortDescending Sort the current column in descending order.
Group Group the current column.
Ungroup Ungroup the current column.
AutoFit Auto fit the current column.
AutoFitAll Auto fit all columns.
ColumnChooser Choose the column visibility.
Filter Show the filter option as given in FilterSettings.Type
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" showColumnMenu="true" allowSorting="true" allowFiltering="true" allowPaging="true" allowGrouping="true"> 
   <e-grid-groupsettings showGroupedColumn="true"></e-grid-groupsettings>
    <e-grid-filtersettings type="CheckBox"></e-grid-filtersettings>   
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID"  width="200"  textAlign="Right" showInColumnChooser="false" ></e-grid-column>        
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right"  format="C2" editType="numericedit" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="300"></e-grid-column>
        <e-grid-column field="ShipCountry"  headerText="Ship Country" visible="false"  width="200" ></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="200"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

You can disable column menu for a particular column by defining the showColumnMenu property of e-grid-column as false.
You can customize the default items by defining the columnMenuItems with required items.

Column menu events

During the resizing action, the grid component triggers the below two events.

  1. The columnMenuOpen event triggers before the column menu opens.
  2. The columnMenuClick event triggers when the user clicks the column menu of the grid.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" showColumnMenu="true" allowSorting="true" allowFiltering="true" allowPaging="true" allowGrouping="true"
columnMenuOpen="columnMenuOpen" columnMenuClick="columnMenuClick"> 
   <e-grid-groupsettings  showGroupedColumn="true"></e-grid-groupsettings>
    <e-grid-filtersettings type="CheckBox"></e-grid-filtersettings>   
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID"  width="200"  textAlign="Right" showInColumnChooser="false" ></e-grid-column>        
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right"  format="C2" editType="numericedit" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="300"></e-grid-column>
        <e-grid-column field="ShipCountry"  headerText="Ship Country" visible="false"  width="200" ></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="200"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function columnMenuOpen() {
        alert('columnMenuOpen event is triggered')
    }

    function columnMenuClick() {
        alert('columnMenuClick event is triggered')
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Custom column menu item

Custom column menu items can be added by defining the columnMenuItems as collection of the columnMenuItemModel.

Actions for this customized items can be defined in the columnMenuClick event.

@{ 

        List<object> columnMenuitems = new List<object>();
        columnMenuitems.Add(new { text = "Clear Sorting", id = "gridclearsorting" });
 }
 <ejs-grid id="Grid" dataSource="@ViewBag.DataSource" columnMenuItems="columnMenuitems"  columnMenuClick="columnMenuClick" showColumnMenu="true" allowSorting="true"  allowPaging="true" >
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID"  width="200"  textAlign="Right" showInColumnChooser="false" ></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right"  format="C2" editType="numericedit" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="300"></e-grid-column>
        <e-grid-column field="ShipCountry"  headerText="Ship Country" visible="false"  width="200" ></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="200"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

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

Customize menu items for particular columns

Sometimes, you have a scenario that to hide an item from column menu for particular columns. In that case, you need to define the ColumnMenuOpenEventArgs.Hide as true in the columnMenuOpen event.

The following sample, Filter item was hidden in column menu when opens for the OrderID column.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" columnMenuOpen="columnMenuOpen" allowFiltering="true" allowGrouping="true" showColumnMenu="true" allowSorting="true"  allowPaging="true" >
     <e-grid-filtersettings type="Menu"></e-grid-filtersettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID"  width="200"  textAlign="Right"  ></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right"  format="C2" editType="numericedit" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name"  width="300"></e-grid-column>
        <e-grid-column field="ShipCountry"  headerText="Ship Country" visible="false"  width="200" ></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City"  width="200"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
<script>
    function columnMenuOpen(args) {
        for (let item of args.items) {
            if (item.text === 'Filter' && args.column.field === 'OrderID') {
                item.hide = true;
            } else {
                item.hide = false;
            }
        }      
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Customize the icon of column menu

You can customize the column menu icon by overriding the default grid class .e-icons.e-columnmenu with a custom property content as mentioned below,

.e-grid .e-columnheader .e-icons.e-columnmenu::before {
      content: "\e969";
}

In the below sample, grid is rendered with a customized column menu icon.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" showColumnMenu="true">
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID"  width="150"></e-grid-column>       
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<style>
   .e-grid .e-columnheader .e-icons.e-columnmenu::before {
        content: "\e969";
    }   
</style>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}