Search results

Grouping

The Grid has options to group records by dragging and dropping the column header to the group drop area. When grouping is applied, grid records are organized into a hierarchical structure to facilitate easier expansion and collapse of records.

To enable grouping in the grid, set the AllowGrouping as true. Grouping options can be configured through the GroupSettings.

tagHelper
group.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="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>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}
  • You can group and ungroup columns by using the groupColumn and ungroupColumn methods.
  • To disable grouping for a particular column, set the Columns.AllowGrouping to false.

Initial group

To apply group at initial rendering, set the column field name in the GroupSettings.Columns.

tagHelper
initial-group.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="true" > 
    <e-grid-groupsettings columns="@(new string[] {"OrderDate"})"></e-grid-groupsettings>
    <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="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>        
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Caption template

You can customize the group caption by using the GroupSettings.CaptionTemplate property.

tagHelper
caption-temp.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="true"  height="315"> 
    <e-grid-groupsettings captionTemplate="#captiontemplate" columns="@(new string[] {"EmployeeID"})"></e-grid-groupsettings>
    <e-grid-columns>
        <e-grid-column field="EmployeeID" headerText="Employee ID" ></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID"></e-grid-column>       
        <e-grid-column field="FirstName" headerText="First Name" width="120"></e-grid-column>
        <e-grid-column field="Title" headerText="Title" width="170"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script id="captiontemplate" type="text/x-template">
    ${field} - ${key}
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Hide drop area

To avoid ungrouping or further grouping of a column after initial column grouping, define the GroupSettings.ShowDropArea as false.

tagHelper
hide-drop-area.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="true"> 
    <e-grid-groupsettings showDropArea="false" columns="@(new string[] {"Freight"})"></e-grid-groupsettings>
    <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="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>        
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Group with paging

On grouping columns with paging feature, the aggregated information and total items are displayed based on the current page. The grid does not consider aggregated information and total items from other pages. To get additional details (aggregated information and total items) from other pages, set the GroupSettings.DisablePageWiseAggregates to false.

If remote data is bound to grid dataSource, two requests will be sent when performing grouping action; one for getting the grouped data and another for getting aggregate details and total items count.

Group by format

By default, a column will be grouped by the data or value present for the particular row. To group the numeric or datetime column based on the mentioned format, you have to enable the EnableGroupByFormat property of the corresponding grid columns.

tagHelper
group-format.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="true" > 
    <e-grid-groupsettings showDropArea="false" columns="@(new string[] { "OrderDate","Freight"})"></e-grid-groupsettings>
    <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="OrderDate" headerText="Order Date" format="yMd" enableGroupByFormat="true" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" format="C2" enableGroupByFormat="true" width="150"></e-grid-column>        
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Grouping events

During the group action, the grid component triggers two events. The ActionBegin event triggers before the group action starts and the ActionComplete event triggers after the group action is completed. Using these events you can perform any action.

tagHelper
grouping-events.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="true" actionBegin="actionHandler" actionComplete="actionHandler"> 
    <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="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>        
    </e-grid-columns>
</ejs-grid>

<script>
function actionHandler(args) { 
    alert(args.requestType + ' ' + args.type); //Custom Action
}
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

The args.requestType is based on the current action name. For example, when grouping, the args.requestType value will be ‘grouping’.

Collapse by external button

You can collapse the selected group from an external button by invoking the expandCollapseRows method.

tagHelper
collapse.cs
<ejs-button id="collapse" content="Collapse" isPrimary="true"></ejs-button>
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource allowGrouping="true" > 
    <e-grid-groupsettings columns="@(new string[] {"Freight"})"></e-grid-groupsettings>
    <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="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>        
    </e-grid-columns>
</ejs-grid>

<script>
document.getElementById('collapse').addEventListener('click', () => {
    var grid = document.getElementById('Grid').ej2_instances[0];
    if (grid.getSelectedRowIndexes().length) {
        var currentTr = grid.getRows()[grid.getSelectedRowIndexes()[0]];
        while (currentTr.classList && currentTr.classList.length) {
            currentTr = currentTr.previousSibling;
        }
        let collapseElement = currentTr.querySelector('.e-recordplusexpand');
        grid.groupModule.expandCollapseRows(collapseElement); //Pass the collapse row element.
    }
});
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

See Also