|
@Html.AntiForgeryToken()
@Html.EJS().Grid("Grid").AllowFiltering(true).DataSource(dm => dm.Url("Home/DataSource").InsertUrl("Home/Insert").UpdateUrl("Home/Update").RemoveUrl("Home/Delete").Adaptor("UrlAdaptor")).Load("load").ActionComplete("actioncomplete").Columns(col =>
{
. . . . . . .
}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()
</div>
</div>
<script>
window.customAdaptor = new ej.data.UrlAdaptor();
customAdaptor = ej.base.extend(customAdaptor, {
processResponse: function (data, ds, query, xhr, request, changes) {
request.data = JSON.stringify(data);
return ej.data.UrlAdaptor.prototype.processResponse.call(this, data, ds, query, xhr, request, changes)
},
insert: function (dm, data, tableName) {
return {
url: dm.dataSource.insertUrl || dm.dataSource.crudUrl || dm.dataSource.url,
data: $.param({
__RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value,
value: data,
table: tableName,
action: 'insert'
}),
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
}
},
update: function (dm, keyField, value, tableName) {
return {
// type: "POST",
url: dm.dataSource.updateUrl || dm.dataSource.crudUrl || dm.dataSource.url,
data: $.param({
__RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value,
value: value,
table: tableName,
action: 'insert'
}),
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
};
},
});
function load(args) {
this.dataSource.adaptor = customAdaptor;
this.dataSource.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];
}
function actioncomplete(args) {
if (args.requestType == 'save' || args.requestType == 'delete') {
this.refresh();
}
}
</script>
|
|
Index.cshtml
@{
Object filterTemplate = new Object();
filterTemplate = (new { read = "read", write = "write" });
}
@Html.EJS().Grid("Grid").AllowFiltering(true).DataSource(dm => dm.Url("Home/DataSource").InsertUrl("Home/Insert").UpdateUrl("Home/Update").RemoveUrl("Home/Delete").Adaptor("UrlAdaptor")).Load("load").ActionComplete("actioncomplete").Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").ValidationRules(new { required = true }).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").FilterBarTemplate(filterTemplate).HeaderText("Customer ID").Width("150").EditType("dropdownedit").Add();
col.Field("EmployeeID").HeaderText("Employee ID").Width("150").Add();
}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()
</div>
</div>
<script type="text/javascript">
function write(args) {
var data = [ { text: "Vinet", value: "Vinet" }, { text: "Hanar", value: "Hanar" }, { text: "Nancy", value: "Nancy" }, { text: "Joe", value: "Joe" },
{ text: "Chops", value: "Chops" }
]
var listObj = new ej.dropdowns.ComboBox({
dataSource: data,
placeholder: 'Select CustomerID',
change: function (args) { //Change event gets triggered when you select the item
read(args) // call the read method to perform filtering
}
});
listObj.appendTo(args.element);
}
function read(args) {
var grid = document.getElementById("Grid").ej2_instances[0];
var pred = new ej.data.Predicate(args.element.id.split('_')[0], "equal", args.element.value); //Generate a predicate
var predicate = pred;
grid.query = new ej.data.Query().where(predicate); //set the predicate to grid query it will send the query to the server side
}
</script>
|
|
HomeController.cs
public ActionResult DataSource(DataManagerRequest Dm)
{
IEnumerable DataSource = getData().ToList();
DataOperations operation = new DataOperations();
if (Dm.Where != null && Dm.Where.Count > 0 && Dm.Where[0].value != null) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, Dm.Where, Dm.Where[0].Operator);
}
else
{
DataSource = getData().ToList();
}
if (Dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, Dm.Skip); //Paging
}
int count = DataSource.Cast<OrderData>().Count();
return Json(new { result = DataSource.Cast<OrderData>().Skip(Dm.Skip).Take(Dm.Take), count = count });
}
|