Articles in this section
Category / Section

How to update only current page selection to server-end using header checkbox in ASP.NET MVC Grid?

3 mins read

This Knowledge base explains how to update only current page selected records to server-end using header checkbox on batch Editing.


Solution:

By default, Grid will select all the page records on clicking the header checkbox. To select only the currentPage records, we have used headertemplate property of the Grid to render the external checkbox in the header which act as header checkbox and handled the checkbox selection only to current page using actionComplete event.

1. Render the Grid control with actionComplete and dataBound event. Initialize the checkbox on column Header using headerTemplateID for type checkbox column.

HTML

<div id="headertemp">
            <input type='checkbox' id='extcheckselectall' />
</div>

JavaScript

<div id=”Grid”></div>
<script type="text/javascript">
        $(function () {
            $("#Grid").ejGrid({
         dataSource: ej.DataManager(window.gridData),                    
                   allowPaging: true,
                   allowGrouping: true,
                   allowSorting: true, 
                   dataBound: "dataBound",
                   actionComplete: "complete",
                    columns: [
                      { field: "Verified", type: "checkbox", headerText: "", headerTemplateID: "#headertemp", width: 50 ,allowSorting: false},
                      { field: "OrderID", isPrimaryKey: true, headerText: "Order ID",  width: 90 },
                      { field: "CustomerID", headerText: "Customer ID",  width: 90 },
                      { field: "EmployeeID", headerText: "Employee ID",  editType: ej.Grid.EditingType.Dropdown, width: 80  },
                      { field: "Freight", headerText: "Freight", width: 80, format: "{0:C}" }
                    ]
                });
            });
   </script>    

 

MVC

RAZOR

 @(Html.EJ().Grid<Object>("Grid")
           .Datasource((IEnumerable<object>)ViewBag.datasource)           
             .AllowPaging()
             .AllowGrouping()
             .AllowSorting()
            .Columns(col =>
              {
                  col.Field("Verified").Type("checkbox").HeaderText("").HeaderTemplateID("#headertemp").Width(90).AllowSorting(false).Add();
                  col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(90).Add();
                  col.Field("CustomerID").HeaderText("Customer ID").Width(90).Add();
                  col.Field("EmployeeID").HeaderText("Employee ID").Width(80).Add();
                  col.Field("Freight").HeaderText("Freight").Width(80).Format("{0:C}").Add();
 
          })
     .ClientSideEvents(eve => { 
            eve.ActionComplete("complete");
            eve.DataBound("dataBound"); 
        }))

 

C#

[Controller]
namespace EJGrid.Controllers  {
    public class HomeController : Controller{
        public ActionResult Index()
        {
            var DataSource = OrderRepository.GetAllRecords();
            ViewBag.datasource = DataSource;
            return View();
        }        
    }
}

 

ASP.NET

ASPX

<ej:Grid ID="Grid" runat="server" AllowPaging="true" AllowGrouping="true" AllowSorting="true">
            <Columns>
                <ej:Column Field="Verified" HeaderText=""  Type="checkbox" AllowSorting="false" HeaderTemplateID="#headertemp" Width="90" />                
               <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True"  Width="90" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="90" />
                <ej:Column Field="EmployeeID" HeaderText="Employee ID"  EditType="Dropdown" Width="80" />
                <ej:Column Field="Freight" HeaderText="Freight"  Width="80" Format="{0:C}"  />                
           </Columns>
           <ClientSideEvents  ActionComplete="complete" DataBound="dataBound"/>
 
 </ej:Grid >

 

ASPX.CS

[CS]
public partial class _Default : Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
            this.Grid.DataSource = new NorthwindDataContext().Orders;
            this.Grid.DataBind();
    }
}     

 

ASP.NET CORE

RAZOR

<ej-grid id="Grid" allow-paging="true" data-bound="dataBound"  datasource="ViewBag.DataSource" action-complete="complete" allow-grouping="true" allow-sorting="true" > 
    <e-columns> 
        <e-column field="Verified" header-text="" header-template-id="#headertemp" type="checkbox" width="90" " allow-sorting="false"></e-column> 
        <e-column field="OrderID" is-primary-key="true" header-text="Order ID" width="90"></e-column>
        <e-column field="CustomerID" header-text="Customer ID" width="90"></e-column>
        <e-column field="EmployeeID" header-text="Employee ID" width="80" edit-type="DropdownEdit"></e-column>
        <e-column field="Freight" header-text="Freight" width="80" format="{0:C}"></e-column>        
    </e-columns>
</ej-grid>  
 

 

C#

 public class GridController : Controller
     {
        public ActionResult GridFeatures()
           {
              var DataSource = new NorthwindDataContext().OrdersViews.ToList();
              ViewBag.DataSource = DataSource;
              return View();
            }
     }

Angular

html
<ej-grid #grid id="Grid" [dataSource]="gridData" [allowPaging]="true" [allowSorting]="true" [allowGrouping]="true" 
         (actionComplete)="complete($event)" (dataBound)="dataBound($event)">
    <e-columns>
        <e-column field="Verified" type="checkbox" headerText="" width="90" headerTemplateID="#headertemp" [allowSorting]="false"></e-column>
        <e-column field="OrderID" [isPrimaryKey]="true" headerText="Order ID" width="90"></e-column>
        <e-column field="CustomerID" headerText="Customer ID" width="90"></e-column>
        <e-column field="EmployeeID" headerText="Employee ID"  width="80"></e-column>
        <e-column field="Freight" headerText="Freight " format="{0:C}" width="80"></e-column>
    </e-columns>
</ej-grid>

 

[ts file]
import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core';
import { EJComponents } from "ej-angular2/src/ej/core";
import { data } from "./data";
@Component({
    selector: 'ej-app',
    templateUrl: 'src/grid/grid.component.html',
})
export class GridComponent {
    public gridData: any;
    constructor() {
        this.gridData = data;
    }
}
 
 

 

2. In the dataBound event of the Grid, render the ejCheckBox and on change event of the checkbox, selected records will be collected and pushed to the getBatchChanges. Later, batchSave method will be called to update the changes to the Grid DataSource. The checkbox state also maintained on Paging, Sorting, Grouping functionalities using the actionCompete event.

JS

 
<script>
    function dataBound(args){
        $("#extcheckselectall").ejCheckBox({
            change: "selectall",// Rendered the ejCheckbox and bind change event
            checked: this.getContent().find("input[checked='checked']").length == this.model.currentViewData.length
        })
    }
    function complete(args) {
        if (!this.initialRender) {     //changed the checkbox state according to paging, sorting, filtering, Grouping
            $("#extcheckselectall").ejCheckBox({
                checked: this.getContent().find("input[checked='checked']").length == this.model.currentViewData.length
            })
        }
    }
    function selectall(args) {
        if (args.isInteraction) {
            var indexes = [];
            var obj = $("#Grid").ejGrid("instance");
            var data = obj.getCurrentViewData();
            for (var i = 0; i < data.length; i++) {
                data[i].Verified = args.isChecked;
            }
            obj.getBatchChanges().changed = data;
            obj.batchSave();
        }
    }
 
</script>

 

 

[ts file]
import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core';
import { EJComponents } from "ej-angular2/src/ej/core";
import { data } from "./data";
@Component({
    selector: 'ej-app',
    templateUrl: 'src/grid/grid.component.html',
})
export class GridComponent {
    @ViewChild('grid') Grid: EJComponents<any, any>;
    public gridData: any;
    dataBound(e: any) {
 
        setTimeout($.proxy(function (arg) {
            var flag = this.Grid.widget.getContent().find("input[checked='checked']").length == this.Grid.widget.model.currentViewData.length;
            $("#extcheckselectall").ejCheckBox({
                change: this.selectAll,
                checked: flag
            })
        },this), 0)
    }
 
    complete(e: any) {
        if (!ej.isNullOrUndefined(this.Grid.widget) && !this.Grid.widget.initialRender) {
            var flag = this.Grid.widget.getContent().find("input[checked='checked']").length == this.Grid.widget.model.currentViewData.length;
            $("#extcheckselectall").ejCheckBox({
                checked: flag
            })
        }
    }
    selectAll(args) {
        if (args["isInteraction"]) {
            var indexes = [];
            var gridObj = $("#Grid").ejGrid("instance");
            var data = gridObj.getCurrentViewData();
            for (var i = 0; i < data.length; i++) {
                data[i].Verified = args.isChecked;
            }
            gridObj.getBatchChanges().changed = data;
            gridObj.batchSave();
        }
    }
}
 
 

 

 

 

 

 

 

Output:

ID

Sample Link:- https://jsplayground.syncfusion.com/xjmsplx2

 

 

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