appComponent.html
<ej-grid #grid [dataSource]="gridData"[allowPaging]="true [editSettings]= "edit" [toolbarSettings]= "tool">
<e-columns>
<e-column field="OrderID" [isPrimaryKey]="true" isIdentity="true" width="75" textAlign="right"></e-column>
<e-column field="EmployeeID" headerText="Employee ID" [validationRules] = "{ required: true, number: true }" width="75" textAlign="right"></e-column>
<e-column field="CustomerID" headerText="CustomerID" width="80"textAlign="right"></e-column>
</e-columns>
</ej-grid>
appComponent.ts
export class AppComponent {
public gridData = ej.DataManager({
url : "Home/DataSource",
batchUrl : "Home/BatchUpdate",
adaptor : "UrlAdaptor"
});
}
Controller side:
public ActionResult BatchUpdate(List<EditableOrder> changed, List<EditableOrder> added, List<EditableOrder> deleted)
{
if (changed != null)
OrderRepository.Update(changed);
if (deleted != null)
OrderRepository.Delete(deleted);
if (added != null)
OrderRepository.Add(added);
var data = OrderRepository.GetAllRecords();
return Json(data, JsonRequestBehavior.AllowGet);
}
|
@(Html.EJ().Grid<object>("Grid")
// .Datasource(d => d.Json((IEnumerable<object>)ViewBag.data).URL("/Home/DataSource"))
.Datasource((IEnumerable<object>)ViewBag.datasource)
.AllowPaging()
.ClientSideEvents(eve =>
{
eve.CellSave("cellsave");
})
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch); })
.Columns(col =>
{
//col.Type("checkbox").Width(50).Add();
col.Field("OrderID").IsPrimaryKey(true).Add();
col.Field("CustomerID").HeaderText("CustomerID").TextAlign(TextAlign.Left).Add();
col.Field("ShipCountry").HeaderText("Ship Country").TextAlign(TextAlign.Left).Add();
col.Field("EmployeeID").HeaderText("Employee ID").Add();
})
)
<script type="text/javascript">
function cellsave(args) {
setTimeout(function (e) {
var obj = $("#Grid").ejGrid("instance");
var records = obj.getBatchChanges();
$.ajax({
url: "/Home/Data",
type: "POST",
datatype: 'json',
contentType: "application/json",
data: JSON.stringify({ value: records.changed, value1: records.added, value2: records.deleted}),
success: function (data) {
alert("Success")
},
error: function (xhr) {
debugger
alert('error');
}
});
},1)
}
</script>
Serverside:-
public ActionResult Data(List<EditableOrder> value, List<EditableOrder> value1, List<EditableOrder> value2 )
{
//return this.Json(new { success = true }, JsonRequestBehavior.AllowGet);
return Json(value, JsonRequestBehavior.AllowGet);
} |