Client side:
window.customAdaptor = new ej.data.UrlAdaptor(); // create the custom adaptor by extending the default UrlAdaptor
document.getElementById('RefreshButton').addEventListener("click", function () {
var grid = document.getElementById("RestockGrid").ej2_instances[0]; // ej2-grid instance
grid.query = new ej.data.Query().addParams('fullRefresh', true);
grid.dataSource = new ej.data.DataManager({ // assigned the new datasource to Grid
url: "/Amazon/RestockData",
adaptor: window.customAdaptor // assign the created custom adaptor
});
});
customAdaptor = ej.base.extend(customAdaptor, {
processResponse(data, ds, query, xhr, request, changes) {
document.getElementById("textbox").value = data.date; // provide the returned date (additional value to textbox)
return data;
}
});
Controller:
public ActionResult RestockData(DataManagerRequest dm, bool fullRefresh = false)
{
...
var dateTime = IniManager.ReadValue(null, "Amazon-Restock", "LastRefresh");
return (
dm.RequiresCounts
? Json(new { result = DataSource, count = viewCount, date = dateTime }, JsonRequestBehavior.AllowGet)
: Json(DataSource, JsonRequestBehavior.AllowGet)
);
} |
customAdaptor = ej.base.extend(customAdaptor, {
processResponse(data, ds, query, xhr, request, changes) {
document.getElementById("textbox").value = data.date; // provide the returned date (additional value to textbox)
var grid = document.getElementById("Grid").ej2_instances[0]; // ej2-grid instance
grid.query.addParams().params[0].value = false;
return data;
}
}); |
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
} |
@{
ViewData["Title"] = "Home Page";
}
@Html.AntiForgeryToken()
<div>
<div>
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").CrudUrl("/Home/CrudUpdate").Adaptor("UrlAdaptor"); }).AllowPaging(true).AllowReordering().Width("auto").Columns(col =>
{
col.Field("OrderID").HeaderText("OrderID").IsPrimaryKey(true).Add();
col.Field("OrderDate").HeaderText("OrderDate").Add();
col.Field("CustomerID").HeaderText("FirstName").Add();
}).Load("load").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>
function load(){
this.dataSource.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];
}
</script>
|
[ValidateAntiForgeryToken]
public IActionResult UrlDatasource([FromBody]Data dm)
{
var order = OrdersDetails.GetAllRecords();
var Data = order.ToList();
int count = order.Count();
return dm.requiresCounts ? Json(new { result = Data.Skip(dm.skip).Take(dm.take), count = count }) : Json(Data);
} |
@{ ViewBag.Title = "Home Page"; } <input type="text" id="textbox" /> <br /> @Html.EJS().Button("RefreshButton").Content("Refresh").CssClass("e-info").Render() @Html.AntiForgeryToken() @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).AllowPaging(true).PageSettings(page => page.PageCount(4).PageSizes(true)).AllowTextWrap().Columns(col => { col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("100").Add(); col.Field("EmployeeID").HeaderText("Employee ID").Width("120").Add(); col.Field("ShipCity").HeaderText("Ship City").Width("120").Format("yMd").Add(); }).Render() <script type="text/javascript"> window.customAdaptor = new ej.data.UrlAdaptor(); // create the custom adaptor by extending the default UrlAdaptor document.getElementById('RefreshButton').addEventListener("click", function () { var token1 = document.getElementsByName("__RequestVerificationToken")[0].value; var gridObj = document.getElementById("Grid").ej2_instances[0]; gridObj.showSpinner(); // show the spinner gridObj.query = new ej.data.Query().addParams('fullRefresh', 1); var Ajax = new ej.base.Ajax({ type: "POST", url: "/Home/UrlDatasource", contentType: 'application/x-www-form-urlencoded; charset=UTF-8', token: token1, data: $.param({ __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value, action: 'insert', additionalParams:1 }), postData: JSON.stringify([{ fullRefresh: 1, memberGroupId: 2, '__RequestVerificationToken': token1, }]) }); Ajax.send(); Ajax.onSuccess = function (result) { debugger; var gridObj = document.getElementById("Grid").ej2_instances[0]; var data = new ej.data.DataUtil.parse.parseJson(result); gridObj.dataSource = data; document.getElementById("textbox").value = data.date; gridObj.hideSpinner(); // We have hide the spinner while bind data in Grid }; Ajax.onFailure = function (data) { console.log(data); gridObj.hideSpinner(); } }); customAdaptor = ej.base.extend(customAdaptor, { beforeSend: function (request, settings) { settings.setRequestHeader("myData1", "Syncfusion"); settings.setRequestHeader("myData2", 23243); }, processResponse(data, ds, query, xhr, request, changes) { document.getElementById("textbox").value = data.date; // provide the returned date (additional value to textbox) return data; } }); </script>
[CONTROLLER]
[ValidateAntiForgeryToken]
public ActionResult UrlDatasource(DataManagerRequest dm, int? additionalParams)
{
IEnumerable DataSource = order;
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Orders>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
var dateTime = DateTime.Now;
return dm.RequiresCounts ? Json(new { result = DataSource, count = count, date = dateTime }) : Json(DataSource);
}
@Html.AntiForgeryToken()
<div>
<div>
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").CrudUrl("/Home/CrudUpdate").Adaptor("UrlAdaptor"); }).AllowPaging(true).Width("auto").Columns(col =>
{ //need to define dataSource as Adaptor type
col.Field("OrderID").HeaderText("OrderID").IsPrimaryKey(true).Add();
col.Field("OrderDate").HeaderText("OrderDate").Add();
col.Field("CustomerID").HeaderText("FirstName").Add();
}).Load("load").PageSettings(page => page.PageSize(15)).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() //you can change PageSize property to customize the number of records get from Controller. Now it will take 15 records
</div>
</div>
...
|
[ValidateAntiForgeryToken]
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = OrdersDetails.GetAllRecords();
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<OrdersDetails>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0) //based on the Skip and Take values, the paging operation is performed. Skip denotes that the number of records need to skiped and the Take denoted that the number od records needs to be taked from server
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
} |
...
<div>
<div>
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").CrudUrl("/Home/CrudUpdate").Adaptor("UrlAdaptor"); }).AllowPaging(true).Query("new ej.data.Query().addParams('ej2Grid', 'true')").Width("auto").Columns(col =>
{
...
}).Load("load").PageSettings(page => page.PageSize(15)).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>
|
[ValidateAntiForgeryToken]
public IActionResult UrlDatasource([FromBody]TestDm dm)
{
IEnumerable DataSource = OrdersDetails.GetAllRecords();
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<OrdersDetails>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
}
public class TestDm : DataManagerRequest
{
public string ej2Grid { get; set; }
} |
|
...
<div>
<div>
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").CrudUrl("/Home/CrudUpdate").Adaptor("UrlAdaptor"); }).AllowPaging(true).Query("new ej.data.Query().addParams('ej2Grid', 'true')").Width("auto").Columns(col =>
{
...
}).Load("load").PageSettings(page => page.PageSize(15)).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>
function load(){
this.dataSource.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];
}
</script> |
[screenshot] |
@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>
...
function load(args) {
this.dataSource.dataSource.headers = [{ 'XSRF-TOKEN': $("input:hidden[name='__RequestVerificationToken']").val() }];
}
</script> |
No records to display |