0

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

Suave Nti
  • 3,721
  • 11
  • 54
  • 78

1 Answers1

3

You main problem is that you ignore the pData of the getData which 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 imgpath which 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 use addJsonData and addXmlData methods to add the data more effectively as you do with respect of addRowData. It is documented here.

I recommend you better instead of modifying of getData function use datatype: 'json' instead of datatype as 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 use loadonce: true parameter 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 SqlDataReader to get the data from the database you can construct the SQL statement (SqlCommand) base on the rows and page parameters 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 TOP and LEFT OUTER JOIN construction. Let us I explain it on an example. For example you need to query Product with the price from the dbo.Products table of the Northwind database. To get first page of data you can use

SELECT TOP(10) ProductID, ProductName, UnitPrice FROM dbo.Products

where 10 you should replace to the value of the rows parameter. To get another page defined by parameter page you need skip (page-1)*rows items and get the next top page items. Using common table expression (CTE) syntax you can write the statement vary easy:

WITH GetAll (Id,ProductName,UnitPrice) AS (
    SELECT ProductID,ProductName,UnitPrice FROM dbo.Products
), GetTop (Id,ProductName,UnitPrice) AS (
    SELECT TOP(20) * FROM GetAll
), GetNext (Id,ProductName,UnitPrice) AS (
    SELECT TOP(10) a.* FROM GetAll AS a
        LEFT OUTER JOIN GetTop AS t ON t.Id = a.Id
    WHERE t.Id IS NULL
)
SELECT * FROM GetNext

You should just replace 10 and 20 on two places above to rows and (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:

SELECT TOP(10) a.* FROM (SELECT ProductID,ProductName,UnitPrice FROM dbo.Products)
        AS a LEFT OUTER JOIN
            (SELECT TOP(20) ProductID,ProductName,UnitPrice FROM dbo.Products) AS t
                ON t.ProductID = a.ProductID
WHERE t.ProductID IS NULL
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hi,Oleg thanks for the quick response. Yes Was using version 3.5. 1 up for explainig the differences between two versions. Also, could you please provide a sample link for Server side pagination? – Suave Nti Dec 12 '11 at 20:12
  • @Samuel: In any way I strictly recommend you [download](http://www.trirand.com/blog/?page_id=6) the last version of jqGrid. The implementation of the paging depend on the technology which you use on the server side. Do you use Entity Framework, LINQ to SQL or `SqlDataReader` for example? – Oleg Dec 12 '11 at 21:17
  • Oleg,I use SqlDataReader. Have downloaded version 4.2 and am trying to get some logic to implement server side paging as notified by you for large datasets. – Suave Nti Dec 12 '11 at 21:22
  • 1
    @Samuel: I updated my answer with an example how you can construct the SQL statements based on `page` and `rows` parameter which get your web server as parameters from jqGrid (see [here](http://stackoverflow.com/a/3161542/315935) an example) – Oleg Dec 12 '11 at 22:39
  • Thanks a lot Oleg, Finally It worked.Your provided links and the above explanation is so clear and straight forward.Thanks a Lot again. – Suave Nti Dec 13 '11 at 13:42
  • @Samuel: You are welcome! I'am glad to know that I could help you. – Oleg Dec 13 '11 at 13:44
  • Hi Oleg,Sorry to trouble you one more time, am using JqGrid 4.2 version with jquery 1.5.2 .. There is an exception being caught every time at this line of jquery 1.5.2 finally { fired = [ context, args ]; firing = 0;} ..any reasons which you ever faced? – Suave Nti Dec 13 '11 at 19:55
  • @Samuel: In the most cases the reason of exception the the wrong order of included JavaScript files. Verify the order based on [the example](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:first_grid#html_file). For troubleshooting I recommend you to use `jquery.jqGrid.src.js` instead of `jquery.jqGrid.min.js`. Moreover it would be better to use JqGrid 4.3, jQuery 1.7.1 and jQuery UI 1.8.17 or 1.8.16. If you would have problems you can open new question and include full HTML and JavaScript code which can be used to reproduce the problem. Try to reduce the example if it's possible. – Oleg Dec 13 '11 at 20:12
  • Sorry Just Realized No need to load jquery-1.5.2.js only jquery-1.5.2-min.js is needed for jqgrid. – Suave Nti Dec 13 '11 at 20:17