Footer Aggregate in ASP.Net Core Grid Component

27 Dec 20227 minutes to read

Footer aggregate value is calculated for all the rows, and it is displayed in the footer cells. Use the footerTemplate property to render the aggregate value in footer cells.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowPaging="true">
    <e-grid-aggregates>
        <e-grid-aggregate>
            <e-aggregate-columns>
                <e-aggregate-column  field="Freight" type="Sum" footerTemplate="Sum:${Sum}"></e-aggregate-column>                        
            </e-aggregate-columns>
        </e-grid-aggregate>
        <e-grid-aggregate>
            <e-aggregate-columns>
                <e-aggregate-column field="Freight" type="Average"  footerTemplate="Average:${Average}"></e-aggregate-column>
            </e-aggregate-columns>
        </e-grid-aggregate>
    </e-grid-aggregates>
    <e-grid-pagesettings pageCount="5"></e-grid-pagesettings>
    <e-grid-columns>                
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" TextAlign="Right" format="C2" width="160"></e-grid-column>
        <e-grid-column field="OrderDate" headerText="Order Date" format="yMd"  width="130"></e-grid-column>               
        <e-grid-column field="ShipCountry"  headerText="Ship Country" width="140"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

NOTE

The aggregate values must be accessed inside the template using their corresponding type name.

How to format aggregate value

You can format the aggregate value result by using the format property.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowPaging="true">
    <e-grid-aggregates>
        <e-grid-aggregate>
            <e-aggregate-columns>
                <e-aggregate-column  field="Freight" type="Sum" format="C2" footerTemplate="Sum:${Sum}"></e-aggregate-column>                        
            </e-aggregate-columns>
        </e-grid-aggregate>
        <e-grid-aggregate>
            <e-aggregate-columns>
                <e-aggregate-column field="Freight" type="Average" format="C2"  footerTemplate="Average:${Average}"></e-aggregate-column>
            </e-aggregate-columns>
        </e-grid-aggregate>
    </e-grid-aggregates>
    <e-grid-pagesettings pageCount="5"></e-grid-pagesettings>
    <e-grid-columns>                
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" TextAlign="Right" format="C2" width="160"></e-grid-column>
        <e-grid-column field="OrderDate" headerText="Order Date" format="yMd"  width="130"></e-grid-column>               
        <e-grid-column field="ShipCountry"  headerText="Ship Country" width="140"></e-grid-column>                
    </e-grid-columns>
 </ejs-grid>
public IActionResult Index()
{
    var Order = Product.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

How to place aggregates on top of the Grid

By default, the aggregated values are placed at the bottom of the footer section. It is possible to place the aggregated values at the top of the header. This is achieved by using the dataBound event, getHeaderContent, and getFooterContent methods of the Grid.

In the following sample, the footer element is appended to the header element using the getHeaderContent and getFooterContent methods in the dataBound event.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowPaging="true" dataBound="dataBound">
    <e-grid-aggregates>
        <e-grid-aggregate>
            <e-aggregate-columns>
                <e-aggregate-column field="Freight" type="Sum" footerTemplate="Sum:${Sum}"></e-aggregate-column>
            </e-aggregate-columns>
        </e-grid-aggregate>
        <e-grid-aggregate>
            <e-aggregate-columns>
                <e-aggregate-column field="Freight" type="Average" footerTemplate="Average:${Average}"></e-aggregate-column>
            </e-aggregate-columns>
        </e-grid-aggregate>
    </e-grid-aggregates>
    <e-grid-pagesettings pageCount="5"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" TextAlign="Right" format="C2" width="160"></e-grid-column>
        <e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="130"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="140"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function dataBound() {
        var grid = document.getElementById("Grid").ej2_instances[0];
        grid.getHeaderContent().append(grid.getFooterContent());
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}