Hi Neil,
Thanks for using Syncfusion products.
Normally when binding grid with the local data from a json array, the whole json data will be provided to the grid initially and no request will be made by datamanager.
But in remote url binding, when allowPaging is set as true in ejGrid, by default the datamanager will fetch the first 12 records. We can define the number of records to be fetched by setting the pageSize property of the pageSettings as follow.
$("#Grid").ejGrid({ dataSource: ej.DataManager({ url:"http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/"}), . . . . . . . allowPaging: true, pageSettings: { pageSize: 10 }, //Setting the pageSize as 10 and hence first 10 records will be requested
});
|
When using the above code snippet, the request send will be as follows.
By default, with allowPaging set as true, the request made will be as follows.
When the Offline property of the ej.DataManager is set to true, the data will be fetched from the server only once; further actions will be performed on the client side. No further request will be made to the server.
We can set the offline property of the datamanager as follow.
$("#Grid1").ejGrid({ dataSource: ej.DataManager({ url:"http://mvc.syncfusion.com/Services/Northwnd.svc/Orders/", offline: true }), . . . . . . .
});
|
The request made when the offline property is set as true is as follows,
Please let us know if we misunderstood you requirement and provide us clarification regarding the highlighted section in the following query.
“then apply them to a DataManager local JSON AJAX request for the data i.e. via a skip and take parameter?”
The information provided would be more helpful for us to analyze the requirement and provide solution as early as possible.
Regards,
Madhu Sudhanan. P
Hi Neil,
Thanks for your valuable update.
To perform paging in grid properly, we should pass the records that we have fetched from the database along with total number of records in the database. For your convenience, we have created a simple MVC sample in which the grid will be loaded with the data from the action “/Grid/GetData” and the same can be downloaded from the below location.
Sample Location: MVCUsingSQLConnection.zip
The Action “GetData” will be as follow.
public JsonResult GetData([Bind(Prefix = "$top")]int top = 24, [Bind(Prefix = "$skip")]int skip = 0) { List<OrderView> list = new List<OrderView>(); int count = 0; string connectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ToString(); using (SqlConnection cn = new SqlConnection(connectionStr)) { cn.Open();
//Getting the records based on skip and top value. SqlCommand cmd = new SqlCommand("SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY OrderID) as row FROM Orders) a WHERE row > " + skip + " and row <=" + (top + skip) + "", cn);
using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) list.Add(new OrderView() { OrderID = (int)dr.GetValue(0), CustomerID = (string)dr.GetValue(1), EmployeeID = (int)dr.GetValue(2) }); }
//Get the total record count SqlCommand cmd1 = new SqlCommand("SELECT COUNT(*) FROM Orders", cn); using (SqlDataReader dr1 = cmd1.ExecuteReader()) { while (dr1.Read()) count = (int)dr1.GetValue(0); }
cn.Close(); } return Json(new { result = list, count= count }, JsonRequestBehavior.AllowGet); //result – records from DB, count-total records in DB }
|
In the above code snippet, you can see that in the result, we have passed the record (total 10 records) along with the total records count (count = 830 in our sample) in the count to the client.
The returned json from the server will be as follow.
When performing paging, based on the “$skip” and “$top” value the corresponding records and the total records count will be returned.
Please let us know if you have any queries.
Regards,
Madhu Sudhanan. P
public JsonResult GetData([Bind(Prefix = "$top")]int top = 24, [Bind(Prefix = "$skip")]int skip = 0)
{
DataTable dt = new DataTable();
int count = 0;
string connectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ToString();
using (SqlConnection cn = new SqlConnection(connectionStr))
{
cn.Open();
//Getting the records based on skip and top value.
using (SqlDataAdapter da = new SqlDataAdapter("SELECT OrderID, CustomerID, EmployeeID FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY OrderID) as row FROM Orders) a WHERE row > " + skip + " and row <=" + (top + skip) + "", cn))
{
da.Fill(dt);
}
cn.Close();
}
var data = DataTableToJson(dt); //get data
return Json(new { result = data, count = count }, JsonRequestBehavior.AllowGet); //result - records, count-total records from database
}
public static IEnumerable<Dictionary<string, object>> DataTableToJson(DataTable dt)
{
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName.Trim(), dr[col]);
}
rows.Add(row);
}
return rows;
} |