Jqgrid & progressive enhancement: Successfully progresses from HTML, to local JSON, to remote JSON, but pager doesn't start correctly?
Asked Answered
E

2

0

Take a look at what happens in my fiddle here: http://jsfiddle.net/tbH5H/

I'm trying to achieve proper progressive enhancement using jgrid. Everything works great, except I don't know how to give jqgrid the correct pager information on first load. My server side script dumps the JSON and a corresponding HTML table for SEO bots. But how can I also give the correct total page count to jqgrid on this first local load? The pager works correctly after a remote data pull as you can see in the fiddle.

HTML

<table id="grid">
    <tr><th>ID</th><th>State</th></tr>
    <tr><td>1</td><td>Alaska</td></tr>
    <!-- etc...server side script dumps this out for SEO... -->
</table>
<div id="pager"></div>  

JS

$("#grid").jqGrid({
    datatype:'local',
    // Server side script dumps this JSON out for first load only, 
    // other requests will come from remote source, see further down...
    data: [{"id":1,"state":"Alabama"},
           {"id":2,"state":"Alaska"},
           {"id":3,"state":"Arizona"},
           {"id":4,"state":"Arkansas"},
           {"id":5,"state":"California"},
           {"id":6,"state":"Colorado"},
           {"id":7,"state":"Connecticut"},
           {"id":8,"state":"Delaware"},
           {"id":9,"state":"Florida"},
           {"id":10,"state":"Georgia"}],  
    height: 250,
    width: 450,
    rowNum:10,
    colNames:['ID','State'],
    colModel:[
       {name:'id',    index:'id',    width:50},
       {name:'state', index:'state', width:100}
    ],
    caption: "States of the USA",
    pager: '#pager'
});
$("#grid").jqGrid('navGrid', '#pager',{edit: false, add: false, del: false, search: false, refresh: true});         

   // Convert the grid to read remotely, but don't trigger a unnecessary reload...
   // (because queries are expensive! We shouldn't need to run them twice!)
   $('#grid').jqGrid("setGridParam",{datatype:"json", mtype:"POST", url:"/some/url/here"});
   $('#grid').jqGrid("setGridParam",{postData:data});
Eleusis answered 2/4, 2014 at 23:22 Comment(2)
Why do you need to to dump the first load? That doesn't look correct to me. Why can't everything be server side?Percolate
How will google know about the data if everything is immediately processed through ajax?Eleusis
E
1

Got it!

I needed to use localReader. See new fiddle: http://jsfiddle.net/2UCk6/

localReader: {
    // These values would be inserted on first page load from server-side script
    page: function(obj) { return 1; },
    total: function(obj) { return 5; }, 
    records: function(obj) { return 50; }
},
Eleusis answered 3/4, 2014 at 3:44 Comment(0)
P
0

Your data should be in the following format:

{ 
  "total": "xxx", 
  "page": "yyy", 
  "records": "zzz",
  "rows" : [{"id":1,"state":"Alabama"},
       {"id":2,"state":"Alaska"},
       {"id":3,"state":"Arizona"},
       {"id":4,"state":"Arkansas"},
       {"id":5,"state":"California"},
       {"id":6,"state":"Colorado"},
       {"id":7,"state":"Connecticut"},
       {"id":8,"state":"Delaware"},
       {"id":9,"state":"Florida"},
       {"id":10,"state":"Georgia"}]
}

Where:

total   =  total pages for the query
page    = current page of the query
records = total number of records for the query
rows    = an array that contains the actual data

You need to have the json reader configured for the above some thing like given below:

 jsonReader : {
     root: "rows",
     page: "page",
     total: "total",
     records: "records",
     repeatitems: false,
     cell: "cell",
     ...
   },
...

More information can be found here with examples - http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data

Update

You can't have the feature when the datatype is local. When the data type is local the page/record etc are ignored by Jqgird. You should probably try virtual scrolling -> Go to Jqgrid Demo -> New in 3.7 -> Virtual Scrolling.

Or probably you should do a server call and return with the data you intend to use as local with the page/records etc?

Percolate answered 2/4, 2014 at 23:35 Comment(7)
Here's an updated fiddle showing you that these adjustments don't seem to work: jsfiddle.net/NJ2nr/3 Any ideas?Eleusis
@DavidGraham - Updated the answer, does that make sense?Percolate
I'm getting close here: jsfiddle.net/NJ2nr/6 You have to click "run" first.Eleusis
@DavidGraham I updated the jsfiddle, now the records came back. jsfiddle.net/NJ2nr/9Percolate
hmmmm...ok...this might work. Let me test this in my app. Be back in a few minutes and I can accept your answer. See! Never give up! :-)Eleusis
seems to only work on jsfiddle, after pressing run. Can't get it to work in my own application...still working on it...Eleusis
It looks like that when setting datatype to "json" the grid makes an initial ajax call. So your fiddle hasn't solved the problem. :-(Eleusis

© 2022 - 2024 — McMap. All rights reserved.