Articles in this section
Category / Section

How to update Summary with unsaved Changes in Batch Edit Mode

1 min read

During batch editing, the summary row will be updated once the update action is completed successfully.

So upon changing any value in batch edit mode, doesn’t alter the corresponding summary row value.

Solution:

We can achieve the above requirement using the CellSave event of the grid.

The CellSave event will be triggered every time a cell value is being altered.

Example:

In the following example, we have rendered a grid with batch editing and summaryRow enabled.

  1. Render the grid

JS:

<div id="Grid"></div>
<script type="text/javascript">
        $(function () {
            var data = window.gridData;
            $("#Grid").ejGrid({
                dataSource: data,
                allowPaging: true,               
                showSummary: true,                
                editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: ej.Grid.EditMode.Batch},
                toolbarSettings: { showToolbar: true, toolbarItems: ["add","edit","delete","update","cancel"] },
                summaryRows: [
                        { title: "Maximum", summaryColumns: [{ summaryType: ej.Grid.SummaryType.Maximum, displayColumn: "EmployeeID", dataMember: "EmployeeID" }]},
                        { title: "Sum", summaryColumns: [{ summaryType: ej.Grid.SummaryType.Sum, displayColumn: "Freight", dataMember: "Freight", format: "{0:C2}" }] }
                    ],
                
                columns: [
                        { field: "OrderID", isPrimaryKey: true, headerText: "Order ID"},
                        { field: "CustomerID", headerText: "Customer ID" },
                        { field: "EmployeeID", headerText: "Employee ID"},
                        { field: "Freight" },
                        { field: "ShipCity"},
                        { field: "ShipCountry"}
                ],
                cellSave: "cellSave"
            });
        });
    </script>

 

MVC

[In View]
 
    @(Html.EJ().Grid<object>("Grid")
          .Datasource((IEnumerable<object>)ViewBag.datasource)
          .ShowSummary()
          .AllowPaging()
          .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch); })
          .ToolbarSettings(toolbar =>
          {
              toolbar.ShowToolbar().ToolbarItems(items =>
              {
                  items.AddTool(ToolBarItems.Add);
                  items.AddTool(ToolBarItems.Edit);
                  items.AddTool(ToolBarItems.Delete);
                  items.AddTool(ToolBarItems.Update);
                  items.AddTool(ToolBarItems.Cancel);
              });
          })
          .SummaryRow(row =>
          {
              row.Title("Maximum")
                 .SummaryColumns(col =>
                 {
                     col.SummaryType(SummaryType.Maximum)
                        .DisplayColumn("EmployeeID")
                        .DataMember("EmployeeID")                        
                        .Add();
                 }).Add();
              row.Title("Sum")
                  .SummaryColumns(col =>
                  {
                      col.SummaryType(SummaryType.Sum)
                         .DisplayColumn("Freight")
                         .DataMember("Freight")
                         .Format("{0:C}")
                         .Add();
                  }).Add();
          })          
          .Columns(col =>
          {
              col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add();
              col.Field("CustomerID").HeaderText("Customer ID").Add();
              col.Field("EmployeeID").HeaderText("Employee ID").Add();
              col.Field("Freight").Add();
              col.Field("ShipCity").Add();
              col.Field("ShipCountry").Add();
          })
          .ClientSideEvents(eve=>eve.CellSave("cellSave"))
          )
 
[In controller]
 
namespace EJGrid.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var DataSource = OrderRepository.GetAllRecords().ToList();
            ViewBag.data = DataSource;
            return View();
        }        
    }
}

 

ASP:

    <ej:Grid ID="Grid" runat="server" AllowPaging="True">
        <ClientSideEvents CellSave="cellSave" />
        <Columns>
            <ej:Column Field="OrderID" IsPrimaryKey="true" />
            <ej:Column Field="CustomerID" HeaderText="Customer ID" />
            <ej:Column Field="EmployeeID" HeaderText="Employee ID" />
            <ej:Column Field="Freight" />
            <ej:Column Field="ShipCity" />
            <ej:Column Field="ShipCountry" />
        </Columns>
        <SummaryRows>
            <ej:SummaryRow Title="Maximum">
                <SummaryColumn>
                    <ej:SummaryColumn SummaryType="Maximum" DisplayColumn="EmployeeID" DataMember="EmployeeID" />
                </SummaryColumn>
            </ej:SummaryRow>
        </SummaryRows>
        <SummaryRows>
            <ej:SummaryRow Title="Sum">
                <SummaryColumn>
                    <ej:SummaryColumn SummaryType="Sum" Format="{0:C}" DisplayColumn="Freight" DataMember="Freight" />
                </SummaryColumn>
            </ej:SummaryRow>
        </SummaryRows>
 
        <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True" EditMode="Batch"></EditSettings>
        <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
    </ej:Grid>
 
[Default.aspx.cs]
protected void Page_Load(object sender, EventArgs e)
        {
            this.Grid.DataSource = OrderRepository.GetAllRecords().ToList();
            this.Grid.DataBind();
        }

 

  1. In the CellSave event of the Grid, using the args parameter the difference in the value updated is calculated and accordingly the sum of the column is calculated and updated. The formatting for the calculated value is done using the formatting method of the Grid.

 

<script type="text/javascript">
            function cellSave(args) {
                if (args.columnName == "Freight") {
                    var newvalue = args.value, format;// getting the new value
                    var oldvalue = args.rowData.Freight;// getting the old value
                    var extra = newvalue - oldvalue;//getting the difference in value
                    for (var i = 0; i < this.model.summaryRows.length; i++)
                        for (var j = 0; j < this.model.summaryRows[i].summaryColumns.length; j++) {
                            if (this.model.summaryRows[i].summaryColumns[j].dataMember == "Freight" && this.model.summaryRows[i].summaryColumns[j].summaryType == "sum"){
                                format = !ej.isNullOrUndefined(this.model.summaryRows[i].summaryColumns[j].format) ? this.model.summaryRows[i].summaryColumns[j].format : "{0:N}";//getting the format of the summaryColumn
                                j = i;// finding the summaryRow to be modified                                
                                break;
                        }
                    }
                    var summary = ($(".e-gridSummaryRows:eq(" + j + ")").find("td.e-summaryrow")[args.cell.index()].innerHTML).replace(/[$,]/g, "")// getting the summaryValue of the corresponding summaryRow
                    var summaryval = (parseFloat(summary) + extra);
                    summaryval = this.formatting(format,summaryval);//get the formatted value of the summary Value
                    $(".e-gridSummaryRows:eq(" + j + ")").find("td.e-summaryrow")[args.cell.index()].innerHTML = summaryval;//assigning the innerHTML of the summaryrow with updated value
                }
            }
            
            </script>

 

Result:

Fig 1: Initial Rendering

 

Fig 2: On editing a cell

Fig 3: On saving the edited cell


Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied