- Auto update aggregate value in batch editing
- Refresh aggregate values in inline editing
Contact Support
Reactive aggregate in ASP.Net MVC Grid component
6 Dec 20243 minutes to read
The Syncfusion® ASP.Net MVC Grid component provides support for reactive aggregates, which allow you to update the aggregate values dynamically as the data changes. Reactive aggregates automatically recalculate their values when there are changes in the underlying data, providing real-time updates to the aggregate values in the grid.
Auto update aggregate value in batch editing
When the grid is in batch editing mode, the aggregate values in the footer, group footer, and group caption are automatically refreshed every time a cell is saved. This ensures that the aggregate values accurately reflect the edited data.
Here’s an example code snippet demonstrating how to auto update aggregate value in batch editing:
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Height("348px").Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(100).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("Freight").HeaderText("Freight").Width("160").Format("C2").EditType("numericedit").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
col.Field("ShipCountry").HeaderText("Ship Country").Width("140").Add();
}).AllowPaging().PageSettings(page => { page.PageCount(5); }).AllowGrouping().GroupSettings(group => { group.ShowDropArea(false).Columns(new string[] { "ShipCountry" }); }).Aggregates(gridAggregation => { gridAggregation.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Sum", GroupFooterTemplate = "Sum: ${Sum}" } }).Add();
gridAggregation.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Average", GroupCaptionTemplate = "Average: ${Average}" } }).Add();
gridAggregation.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Sum", FooterTemplate = "Sum: ${Sum}" } }).Add(); }).Toolbar(new List<string>()
{ "Delete", "Update", "Cancel" }).EditSettings(edit => { edit.AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Batch); }).Render()
public IActionResult Index()
{
ViewBag.dataSource = OrderDetails.GetAllRecords();
return View();
}
Adding a new record to the grouped grid will not refresh the aggregate values.
Refresh aggregate values in inline editing
By default, reactive aggregate update is not supported by inline and dialog edit modes as it is not feasible to anticipate the value change event for every editor. But, you can refresh the aggregates manually in the inline edit mode using the refresh method of aggregate module.
In the following code, the input event for the Freight column editor has been registered and the aggregate value has been refreshed manually.
@Html.EJS().Grid("grid").DataSource((IEnumerable<object>)ViewBag.dataSource).ActionBegin("actionBegin").Height("348px").Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(100).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("Freight").HeaderText("Freight").Width("160").Format("C2").EditType("numericedit").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Edit(new{ @params = new Syncfusion.EJ2.Inputs.NumericTextBox(){Change = "changeFunction"}}).Add();
col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
col.Field("ShipCountry").HeaderText("Ship Country").Width("140").Add();}).AllowPaging().Aggregates(gridAggregation =>{gridAggregation.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Sum", FooterTemplate = "Sum: ${Sum}" } }).Add();}).Toolbar(new List<string>(){"Edit","Update","Delete", "Cancel" }).EditSettings(edit => { edit.AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal);
}).Render()
<script>
var selectedRecord = {};
function actionBegin(args) {
if ((args.requestType === 'beginEdit')) {
selectedRecord = {};
selectedRecord = args.rowData;
}
}
function changeFunction(args) {
var gridInstance = document.getElementById("grid").ej2_instances[0];
selectedRecord['Freight'] = args.value;
gridInstance.aggregateModule.refresh(selectedRecord);
}
</script>
public IActionResult Index()
{
ViewBag.dataSource = OrderDetails.GetAllRecords();
return View();
}