问题
jqGrid is powered by remote json data in ASP .NET MVC2 application. On page load two requests are sent to server: one to retrieve whole html page with colmodel and second invoked by jqgrid to retrieve data.
colmodel is stored in database and depends on user rights and user configuration. Creating colmodel requires number of sql server calls which take a while.
Both request require building colmodel in server. For data retrieval colmodel is required to get correct number of columns to build select statement.
Currently this colmodel is built two times for every request. Also total number of recods is required to be returned which is slow on large data (causes whole result scan in PostgreSql server).
How to speed the things up ? How to build colmodel only once and send it and data in same request?
回答1:
I agree that extension of jqGrid to support the loading of some parts of colModel
per one Ajax will be very helpful. For about a year I posted the feature request for example.
What you can do now:
- If I correct understand your requirements you need to show the user the subset of the columns depend on the user's permissions. One can implement the requirement in one Ajax request. What you can do is: first don't send the "hidden" data or send there as empty string. Seconds you can send the list of columns, which should be hidden, to the client. In the case you can implement the variable number of columns in jqGrid. You can send the information inside of
userData
part of JSON response for example. To have better performance with many hidden columns I would recommend you to callshowCol
orhideCol
inside ofbeforeProcessing
and hide/show the columns on the empty grid. It will speed up the performance ofshowCol
orhideCol
dramatically. If it's needed you can include additional call ofclearGridData
. - You have to optimize the retrieval
colModel
. I don't understand why it should be slow. All depends from your implementation. In any way I am sure that one can make the retrieval really quickly. - To improve the performance of request of data from the database you can consider don't fill
records
field of the JSON response and settotal
topage
+ 1. It enebles the "Next" button of the pager. You should settotal
equal topage
only if you returns less rows as therows
(number of rows per page). In the most cases it will be good criteria to detect the last page. You can additionally hide some field on the pager lake the "Last" button and thesp_1_...
span which shows the total number of pages. You can do this by the usage ofpgtext : "Page {0}"
option or the usage ofpginput: false
to have no pager input at all. Theviewrecords
should befalse
(its default value). After all the customization you will don't need to calculate the total number of records and in the way improve performance of the database request in case of large data.
来源:https://stackoverflow.com/questions/9757041/how-to-retrieve-colmodel-and-data-on-single-request-and-speed-up-jqgrid-load