am trying to add jQuery Grid into my application(C# and Asp.net) using samples provided in some blogs, able to use Json data sent by Webservice.
Now have tried to add pagination for the Grid and got strucked.Script is like this.
<script type="text/javascript">
$(function () {
$("#table").jqGrid({
datatype: function (pdata) { getData(pdata); },
height: 250,
colNames: ['ID', 'First Name', 'Last Name'],
colModel: [
{ name: 'ID', width: 60, sortable: false },
{ name: 'FirstName', width: 200, sortable: false },
{ name: 'LastName', width: 200, sortable: false }
],
imgpath: '<%= ResolveClientUrl("styles/redmon/images") %>',
pager: jQuery('#pager'),
rowNum: 2,
rowList: [2, 5, 10, 50, 100, 200, 500, 1000],
height: "100%",
viewrecords: true,
scrollOffset: 0,
caption: 'Sample'
});
});
function getData(pData) {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: '<%= ResolveClientUrl("~/WebService.asmx/GetListOfPersons") %>',
data: '{}',
dataType: "json",
success: function (data, textStatus) {
if (textStatus == "success")
ReceivedClientData(JSON.parse(getMain(data)).rows);
},
error: function (data, textStatus) {
alert('An error has occured retrieving data!');
}
});
}
function ReceivedClientData(data) {
var thegrid = $("#table");
thegrid.clearGridData();
for (var i = 0; i < data.length; i++)
thegrid.addRowData(i + 1, data[i]);
}
function getMain(dObj) {
if (dObj.hasOwnProperty('d'))
return dObj.d;
else
return dObj;
}
</script>
…html block
<table id="table" cellpadding="0" cellspacing="0">
</table>
<div id="pager" class="scroll" style="text-align:center;"></div>
The Pager div is displayed and attached but isnt working am I missing something?
Thanks
Samuel
You main problem is that you ignore the
pDataof thegetDatawhich can be forwarded to your ASMX web service.You use very old template for your jqGrid. The current version of jqGrid now 4.3 and you use still
imgpathwhich was already deprecated in the version 3.5 (see the documentation). Very old version of jqGrid had no good support for calling of Web services, but even at the time one could already useaddJsonDataandaddXmlDatamethods to add the data more effectively as you do with respect ofaddRowData. It is documented here.I recommend you better instead of modifying of
getDatafunction usedatatype: 'json'instead ofdatatypeas function. In the old demo for example you can find an example how to implement this exactly. In another answer you can find an example how to useloadonce: trueparameter in case if you prefer don’t implement data paging on the server and instead of that want send all the grid data to the client side and allow jqGrid do paging, sorting and filtering the data for you on the client side. It can work effective only with relatively small number of rows (some hundred rows for example).UPDATED: If you use
SqlDataReaderto get the data from the database you can construct the SQL statement (SqlCommand) base on therowsandpageparameters which you receive from the server.In the most cases you need query the data which has unique ids. So you can implement paging using of
SELECT TOPandLEFT OUTER JOINconstruction. Let us I explain it on an example. For example you need to query Product with the price from thedbo.Productstable of the Northwind database. To get first page of data you can usewhere
10you should replace to the value of therowsparameter. To get another page defined by parameterpageyou need skip(page-1)*rowsitems and get the next toppageitems. Using common table expression (CTE) syntax you can write the statement vary easy:You should just replace
10and20on two places above torowsand(page-1)*rows. If you has some database which not support common table expression (CTE) you can rewrite the same query with respect of subqueries: