Hi Team,
I am using Javascript ejGrid to display data dynamically.
At the initial load it loads data from the server, but it fails to load data after a call through Ajax call, below is my source code
The below code does not throws any error, and data is till empty.
But the server call has data.
Client Side:<script type="text/javascript">
$.ajaxPrefilter(function (options, original_Options, jqXHR) {
options.async = true;
});
function RefreshGrid(data) {
var gridData = ej.parseJSON(data);
var gridModel = $("#Grid").ejGrid("model");
//$("#Grid").data("ejGrid") !== undefined && $("#Grid").ejGrid("destroy")// destroy the grid if already rendered
gridModel.query = new ej.Query();//clear the queries
gridModel.dataSource = gridData;//binding the data to grid
$("#Grid").ejGrid(gridModel);
var gridObj = $("#Grid").data("ejGrid");
// Refreshes the grid data source
gridObj.dataSource(data); // tried no use
gridObj.dataSource(data, true); // tried no use
gridObj = $("#Grid").ejGrid("instance"); // tried no use
gridObj.dataSource(data); // tried no use
gridObj.dataSource(data, true); // tried no use
alert("Refreshed...");
}
var successMessage = function (data) {
console.log("Data:" + data);
RefreshGrid(data);
console.log("Data Loaded to Grid...");
}
var errorMessage = function (error) {
console.log("Error :" + error);
}
$("#getData").click(function () {
//JsUpdate('http://localhost:2422/Orders?test=data&t1=44&ship_country=India',
// "GET", "", "", successMessage, errorMessage); // Not works.
FilterGridData(); // This works Perfectly
//ref: https://www.syncfusion.com/kb/5963/how-to-send-custom-headers-to-server-using-datamanager
});
function FilterGridData() {
var dataManager = ej.DataManager({
url: "http://localhost:2422/Orders?test=data&t1=44&ship_country=India",
enableCaching: true,
cachingPageSize: 10,
timeTillExpiration: 120000,
adaptor: new ej.WebApiAdaptor()
});
$("#Grid").ejGrid({
dataSource: dataManager,
rowSelected: "rowSelected", // "rowSelected" is a function
allowPaging: true,
pageSettings: { pageSize: 10 }, //pageSizeList: [5, 10, 15, 20]
columns: [
{ field: "OrderID", isPrimaryKey: true },
{ field: "CustomerID" },
{ field: "Freight", editType: "numericedit" },
{ field: "EmployeeID" },
{ field: "ShipCountry" }
],
load: function (args) {
//this.model.dataSource.dataSource.headers = [];//So define them as array
//this.model.dataSource.dataSource.headers.push({ "fromDate": "12-06-2017" });//pushing Some JSON Object
//this.model.dataSource.dataSource.headers.push({ "toDate": "18-06-2017" });//pushing Some JSON Object
}
});
}
function LoadAllGridData() {
var dataManager = ej.DataManager({
url: "http://localhost:2422/Orders",
enableCaching: true,
cachingPageSize: 10,
timeTillExpiration: 120000,
adaptor: new ej.WebApiAdaptor()
});
$("#Grid").ejGrid({
dataSource: dataManager,
rowSelected: "rowSelected", // "rowSelected" is a function
allowPaging: true,
pageSettings: { pageSize: 10 }, //pageSizeList: [5, 10, 15, 20]
columns: [
{ field: "OrderID", isPrimaryKey: true },
{ field: "CustomerID" },
{ field: "Freight", editType: "numericedit" },
{ field: "EmployeeID" },
{ field: "ShipCountry" }
],
load: function (args) {
this.model.dataSource.dataSource.headers = [];//So define them as array
this.model.dataSource.dataSource.headers.push({ "fromDate": "12-06-2017" });//pushing Some JSON Object
this.model.dataSource.dataSource.headers.push({ "toDate": "18-06-2017" });//pushing Some JSON Object
}
});
}
function rowSelected(args)
{
var selectedrowindex = this.selectedRowsIndexes; // get selected row indexes
console.log("Selected Index: " +selectedrowindex);
var selectedrecords = this.getSelectedRecords(); // get selected records
}
$("#Grid").dblclick(function () {
var gridObj = $("#Grid").ejGrid("instance");
var selectedrecords = gridObj.getSelectedRecords();
if (selectedrecords[0] != undefined) {
//Edit operation here
console.log(selectedrecords[0])
gridObj.refreshContent();
}
console.log("Ready to Edit");
});
$('#Grid').keyup(function (e) {
if (e.keyCode == 46) {
var gridObj = $("#Grid").ejGrid("instance");
var selectedrecords = gridObj.getSelectedRecords();
if (selectedrecords[0] != undefined) {
//Delete operation here
console.log("Ready to Delete :" + selectedrecords[0])
gridObj.refreshContent();
}
console.log('Delete key released');
}
});
$(function () {
LoadAllGridData(); // Works When page Load.
});
</script>
Server Side:[Route("Orders")]
[HttpGet]
public object GetObject()
{
var queryString = HttpContext.Current.Request.QueryString;
int skip = Convert.ToInt32(queryString["$skip"]);
int take = Convert.ToInt32(queryString["$top"]);
if (string.IsNullOrEmpty(queryString["ship_country"]))
{
var data = _lst.Skip(skip).Take(take).ToList();
_lstCurrent = data;
}
else
{
string shipCountry = queryString["ship_country"].ToString();
var filterData = _lst.Where(ord => ord.ShipCountry.Equals(shipCountry)).ToList();
var data = _lst.Skip(skip).Take(take).ToList();
_lstCurrent = filterData;
}
return new
{
Items = _lstCurrent,
Count = _lstCurrent.Count
};
}
I have also attached Server side code and client side code for your reference..
Please let me know what went wrong in my code.
Thanks in advance.
Attachment:
Sync_7f7466f4.zip