I don't want to hit the server and bring back every row when I am paging through the records by using the pager. I read that if I set the datatype = local in the complete blog in the .ajax function AND if I set loadonce:true then I should be able to avoid having to wait for the grid to reload with the data.
However, when I do these things the grid doesn't go to the next page. It just hangs...
What am I doing wrong?
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
datatype: processrequest,
mtype: 'POST',
jsonReader: {
root: "rows", //arry containing actual data
page: "page", //current page
total: "total", //total pages for the query
records: "records", //total number of records
repeatitems: false,
id: "ID" //index of the column with the PK in it
},
colNames: ['Name', 'Title'],
colModel: [
{ name: 'name', index: 'name', width: 250 },
{ name: 'title', index: 'title', width: 250 }
],
pager: '#pager',
rowNum: 10,
rowList: [10, 20, 30],
sortorder: "desc",
viewrecords: true,
height: '250px',
caption: 'My first grid',
loadonce: true
}).navGrid('#pager', {edit: false, add: false, del: false});
});
function processrequest(postdata) {
...
$.ajax({
...
complete: function (jsondata, stat) {
if (stat == "success") {
var thegrid = jQuery("#list2")[0];
var jsonObject = (eval("(" + jsondata.responseText + ")"));
thegrid.addJSONData(jsonObject.d);
$(".loading").hide();
} else {
$(".loading").hide();
alert("Error with AJAX callback");
}
$("#list").setGridParam({ datatype: 'local' });
}
});
}
There are some misunderstandings. If you use
datatype: local
then you have to fill jqGrid yourself with methods likeaddRowData
or set the data in once withdata
parameter (for jqGrid version 3.7 and higher). So the usage ofdatatype: local
follows to jqGrid don't load any data itself and yourdatatype: processrequest
parameter will be ignored.If you want to use
loadonce: true
parameter which is supported since version 3.7 of jqGrid, you should have all parameters of jqGrid for JSON or XML (for exampledatatype: json
in your case) and an additional parameterloadonce: true
. Then after the first load of data jqGrid will switch the datatype todatatype: local
and after that it will work independent on server but ignore some parameters (likedatatype: processrequest
in your case).One more small remark. The most properties of
jsonReader
which you use in your example are default (see this wiki). The parameters which you use will be combined with the default properties, so it is enough to use parameter likejsonReader: { repeatitems: false, id: "ID"}
UPDATED: OK Jeff. It seems to me, to solve your problem you need some more code examples from both sides: client and server. Here is a small example which I created and tested for you.
First of all the server side. In the ASMX web service we define a web method which generate a test data for your table:
where classes
JqGridData
andTableRow
are defined like following:Here you can see, the web method
TestMethod
has no parameters and posts back the full data. Paging, sorting and searching of data will be done by jqGrid (version 3.7 or higher).To read such data and put into jqGrid we can do following:
Some comments about the definition of jqGrid:
To communicate with ASMX web service through JSON one needs to do the following in the corresponding
jQuery.ajax
request:dataType: 'json'
must be set.contentType:'application/json; charset=utf-8'
must be set.To do all these I use
datatype
,ajaxGridOptions
andserializeGridData
parameters of jqGrid. I do JSON encoding withJSON.stringify
function (the corresponding JavaScript can be downloaded from here).Then the received data must be decoded. I do this with my favorite feature of jqGrid -
jsonReader
with functions (see this SO post and this wiki).At the end we use
loadonce: true
which change thedatatype
of jqGrid from'json'
to'local'
and we can use immediately all advantage of local paging, sorting and advanced searching existing since jqGrid version 3.7.If you do want make server side paging, sorting and searching (or advanced searching) with ASMX web service it is also possible. To save a little place here and to separate code examples I will post the corresponding example in your other question jqgrid Page 1 of x pager (see UPDATED part).
It's a little bit late, but here's a (the?) super-easy solution for any future solution-seekers:
That's it. I'm using 3.7.2, can't speak for any other versions. The problem (apparently) stems from 'loadonce' only working with the pre-defined datatype values, which a function is not. I believe the other built-in values will also work, but 'local' makes sense.
This worked for me. I was having an issue with paging and sorting not working. Probably because of the .d and __type items that were being sent back in the JSON object in .net. However, with the extra configurations in this example. This worked !
I was going nuts. This is the way to configure the grid if you are using .Net as your webservice. It's configured to parse out and correctly set the data elements from the JSON object into the correct locations needed in the Grid to allow for the paging and sorting to work.
I had to comment, because I'm sure there are a few people out there who would like to use this Grid but are using .Net as their webservice.