F.N
F.N

Reputation: 401

JqGrid pager loads same data in every page

Data loaded as I expected. Pages counted too. But when I change the page number same data appears.

Here is the jQgird code:

$("#list").jqGrid({
        url: "classes/classesController.php",
        datatype: "json",
        mtype: "POST",
        postData: {method:"getLogsList"},
        colNames: ["Id","Ip", "Hostname", "Log", "Ημ/νια"],
        colModel: [
            { name: "log_id", index:"log_id", width: 100, sortable:false, hidden:true},
            { name: "ip", index: "ip", width: 130, searchoptions:{sopt:['eq']} },
            { name: "hostname", index:"hostname", width: 180 , searchoptions:{sopt:['eq']}},
            { name: "log", index:"log", width: 180, align: "right", searchoptions:{sopt:['eq']}},
            { name: "date", index:"date", width: 180, align: "right", searchoptions:{sopt:['eq']}}
        ],
        jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatitems: true, cell: "cell",  id: "id",},
        caption: "Logs",
        gridview: true,
        rownumbers: true,
        height: 'auto',
        shrinkToFit: false,
        rowNum:2,
        pager: '#gridpager'
    }); 

and here the php part :

init_mysql();

        $response = new stdClass();


        // Getting pages number (for jqGrid pager)

        if(!$sidx) $sidx =1;
        $result = mysql_query("SELECT COUNT(*) AS count FROM logs");
        $row = mysql_fetch_array($result);
        $count = $row['count']; 

        if( $count >0 ) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;

        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $count;


        // Getting data for jqGrid table

        $data =  mysql_query("SELECT * FROM logs ORDER BY log_id DESC");
        $i = 0;

        if(mysql_num_rows($data))
        {
            while($row = mysql_fetch_array($data)) 
            { 

                $response->rows[$i]['id']=$i+1; 

                $response->rows[$i]['cell']=array('log_id'=>$row['log_id'],'ip'=>$row['ip'],'hostname'=>$row['host'],'log'=>$row['input'],'date'=>$row['date']);

                $i++;
            }   
        }

        echo json_encode($response);

Why this happens ? As I saw in the request all works fine. page one became page 2 and so on..

method=getLogsList&_search=false&nd=1371379260922&rows=33&page=2&sidx=&sord=asc

Upvotes: 0

Views: 2314

Answers (2)

Oleg
Oleg

Reputation: 221997

Short answer would be: you should add loadonce: true option to fix the problem or change your server code to implement server side paging.

jqGrid support two styles of division of labor between the server and the client side. Old versions of jqGrid (before version 3.7) had support only server side paging, sorting and filtering of data. You don't use loadonce: true option, so you should follow the agreement:

  • jqGrid send to the server (to url) parameters rows, page, sidx and sord. For example sidx=ip, sord=asc, rows=10, page=2. It means that the server have to sort the full dataset by ip column in ASC order and returns back only the second page (page=2) of the data. The page size are defined by rows parameter. If rows=10 then one have to skip the first 10 items and return the next 10 items (or less if the total number of the items are less then 20). The exact SQL statement depends on the SQL server which you use. One can use LIMIT, TOP and so on constructions. The returned data should includes records - the total number of items, total - the number of pages calculated based on tne page size rows. The page of the server response is optional. Typically it's the same page number which was requested, but in general the server can detect the changing of total number of items and returns another page. For example if the user requested the page 100 and one have only 7 pages then the server can detect the problem and returns the past page number 7. In the case the property page of the server response should be set to 7 - the number of returned page.

If you need display the dataset in the grid which is not so large (for example some hundred items) then it could be more effective to use loadonce: true option of jqGrid. In the case the server should just sort the data and returns all sorted items. After the first loading the datatype option of the grid will be automatically changed to "local". jqGrid displays only the first page of returned data, but it hold all returned items. The later sorting, paging and searching/filtering will be implemented locally without any communication with the server. Because JavaScript engine of modern web browsers are very quickly, then paging of relatively large set of data could be much quickly as sending of requests to the server.

Upvotes: 3

F.N
F.N

Reputation: 401

Ok. Finally i found the answer. The problem was the loadonce. And it has to be set true

loadonce: true

Upvotes: 0

Related Questions