<Grid>
<input type="button" id="btn" value="getSelectedRecords"/>
@(Html.EJ().Grid<object>("UserGrid")
.Datasource((IEnumerable<object>)ViewBag.datasource)
.AllowScrolling()
.AllowPaging() /*Paging Enabled*/
.SelectionType(SelectionType.Multiple)
. . .
.ClientSideEvents(eve => eve. ActionComplete("actionComplete"). ActionBegin("actionBegin").DataBound("dataBound").TemplateRefresh("TemplateRefresh"))
.Columns(col =>
{
col.HeaderText("Checkbox selection").Template(" <input type='checkbox' class='rowCheckbox' />").Width("80px").TextAlign(TextAlign.Center).Add();
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
. . .
}))
<script type="text/javascript">
//DataBound event
function dataBound(args) {
$("#UserGrid .rowCheckbox").ejCheckBox({ "change": checkChangeMethod });
this.model.records = {}; /* Additional property for getSelected records*/
this.model.selectedIndex = {}; /* Additional property for getSelected record indexes*/
}
//TemplateRefresh
function TemplateRefresh(args) {
$("#UserGrid .e-templatecell .rowCheckbox").ejCheckBox({ "change": checkChangeMethod }); // Render the Checkbox control in Grid
}
//Checkbox check change
function checkChangeMethod(e) {
gridObj = $("#UserGrid").data("ejGrid");
var rowCheck = $(".rowCheckbox:checked");
var trueCheckBox = $("#UserGrid .e-templatecell .rowCheckbox").parent("span[aria-checked='true']").parent("td");
var falseCheckBox = $("#UserGrid .e-templatecell .rowCheckbox").parent("span[aria-checked='false']").parent("td");
trueCheckBox.addClass("e-selectionbackground e-active");
trueCheckBox.siblings().addClass("e-selectionbackground e-active");
falseCheckBox.removeClass("e-selectionbackground e-active");
falseCheckBox.siblings().removeClass("e-selectionbackground e-active");
gridObj.multiSelectCtrlRequest = true;
}
//ActionBegin
function actionBegin(args) {
if (args.requestType == "paging") {
if (this.selectedRowsIndexes.length > 0)
this.model.records[args.previousPage] = this.getSelectedRecords(); //Store the selected records
this.model.selectedIndex[args.previousPage] = this.selectedRowsIndexes; // Store the selectedRow indexes
}
}
//ActionComplete
function actionComplete(args) {
if (args.requestType == "paging") {
var indx = this.model.selectedIndex[args.currentPage]; // Get the current page selected row indexes
for (var i = 0; i < (indx || []).length; i++) {
$("#UserGrid .rowCheckbox").eq(indx[i]).ejCheckBox("model.checked", true)
this.selectRows(indx[i]); // Select the rows for previously selected records
}
}
}
//Get All selected records
$("#btn").ejButton({ click: "getMergedSelected" });
function getMergedSelected() {
var obj = $("#UserGrid").ejGrid("instance");
var r = obj.model.records, selected = [];
for (var p in r)
{
ej.merge(selected, r[p]);
}
ej.merge(selected, obj.getSelectedRecords());
var rowID = [];
for (count = 0; count < selected.length; count++) {
rowID.push(selected[count]["OrderID"]);
}
ej.distinct(rowID);
return rowID; // Returns the all selected row primarykey value
}
</script>
|