简体   繁体   中英

JQGrid Get page level data at a time

Currently i have implemented jqgrid which fetches data from DB and returning the JSON data to JQGRID.

JQGrid call

        rowNum: 10,
        rowList: [5, 10],
        url: "/Home/GetDataFromEntity"

Data returned from C#

return Json(result, JsonRequestBehavior.AllowGet);

What am trying is if the page has 10 records only that 10 records i want to bring from DB and if they click to next page i want to get the next 10 data because if the data is huge i dont want to bring all data to memmory which i think will be a performance Hit.

How can i implement this ?

Thanks

This is fairly straightforward. Basically all you need to do is implement paging, where you are only asking the DB for the page of data that you are going to display. You will see jqGrid will provide this information to your controller so you can use then when retrieving data.

The controller will take this data in via something like (I don't know your back end tech stack so here is C# code) this:

public ActionResult GridDataFetch(string sidx, string sord, int page, int rows, bool _search, string filters)
{
....

Then when you go to retreive your data, you can ask the database for the page of data that your user wants without having to retreive the whole dataset. This can be more complicated then it seems but for the basics it is as simple as somthing like (again C# code)

var pagedQuery = dataset.OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows);

You can see above we order the data in the manner that the user specified and jqGrid passed along with sidx & sord and then we skip all the records before the page we are interested in via the skip , and then we take the rows we are interested in. This again was a C# method for grabbing a page of data but the basics should be there for any setup. As a side note if you do any filtering via the grid, or other logic you would have filtered your dataset prior to this call.

You would then pass this paged query same as you would usually do in JSON.

you have to implement server side pagination to achieve this.

I have implemented in java like this: (you are using c#)

int limit = Integer.parseInt(request.getParameter("rows")); // get how many rows we want to have into the grid
String sidx = request.getParameter("sidx"); // get index row - i.e. user click to sort
String sord = request.getParameter("sord"); // get the direction
int start = (limit* page) - limit;
String rows = request.getParameter("rows");

String query = "select * from ( select a.*, ROWNUM rnum from ( select * from CRM_PROT_STAGES  where  PROTOCOL_ID = '"+param +"' ) a where ROWNUM <= "+ limit +")where rnum  >="+start;

use above paarameters, and then put the condition in query as above

You need to use paging for the same. You can use .Take and .Skip methods at server side

After you get your resultset you can do as following

var smallResultSet = fullResultSet.Skip(request.PageIndex * request.RecordsCount).Take(request.RecordsCount).ToList();

here I've made assumption that you are fetching your resultset in fullResultSet variable than filter it and store it in smallResultSet . request parameter will be passed when you bind grid to controller action.

After that iterate through smallResultSet and create your JSONResult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM