2

I'm using jqgrid 3.8.2, I'm trying to use below code to reload data from server side and show specific page, like current page. $("#mygrid").setGridParam({datatype:json}).trigger("reloadGrid",[{page:5}]); grid could load data from server properly, but always show first page instead of page 5. anybody could give me a hand on it?

Regards Simon

LeftyX
  • 35,328
  • 21
  • 132
  • 193
YunFeng
  • 65
  • 1
  • 8
  • can you show us your server-side code? Anyway, to reload your jqGrid this is enough: $("#mygrid").trigger('reloadGrid',[{page:5}]); – LeftyX Jul 05 '11 at 08:52

1 Answers1

11

I suppose that you use loadonce: true parameter. To reload the data from the server you set datatype: to 'json' (I hope that you use setGridParam({datatype:'json'}) and not setGridParam({datatype:json}) like it is in the code fragment from the question). After the data will be loaded from the server the first page of the local data will be displayed.

To solve the problem you will have to reload the grid one more time inside of loadComplete, but now you should reload the local grid. To have no reloading loop and to allow local paging you should verify whether the current datatype is 'json':

var myGrid = $("#mygrid"), currentPage = 1;
...
myGrid.jqGrid({
    // all grid parameters and additionally the following
    loadComplete: function() {
        if (this.p.datatype === 'json' && currentPage !== 1) {
            setTimeout(function() {
                myGrid.trigger("reloadGrid",[{page:currentPage}]);
            }, 50);
        }
    }
});
....
currentPage = 5;
myGrid.setGridParam({datatype:'json'}).trigger("reloadGrid",[{page:currentPage}]);

See the demo here.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I am trying to get this to work too - but I am not using loadonce. I am trying to set the current page when the user first comes to the page. On my server the current page - "page" - is always one, no matter what I set the grid param as in loadComplete. – Cody Jan 05 '15 at 19:26
  • @DoctorOreo: Do you tried just to use `page: 2` (any value other as default `page:1`) option of jqGrid? You should verify that jqGrid will send `page=2` as the parameter to the server. If it will not solve the problem you should open new question, include more code and description of the problem. – Oleg Jan 05 '15 at 20:38
  • @Oleg: I posted a [new question](http://stackoverflow.com/questions/27787867/setting-page-of-jqgrid-on-first-load) as I did get it to partially work but not as I had hoped. – Cody Jan 05 '15 at 21:05
  • @Oleg : Great Answer, but please can you tell me if I have called a ajax request that returns me json data. At this time, how can I fill my jqgrid with this data? I mean what function should I use after calling this request to fill the jqgrid. Thanks. – Jad Chahine Apr 11 '16 at 07:46
  • @JadChahine: It's very old answer. Now there are better possibilities. Answer on your question: The grid have `url` parameter. Usage of `.trigger("reloadGrid", {fromServer: true, page:currentPage});` will reload the grid with the new date from `url` instead of reloading the grid with probably modified locally `data` parameter of jqGrid (in case of usage without `fromServer: true` option of [free jqGrid](https://github.com/free-jqgrid/jqGrid)). Moreover `forceClientSorting: true` option allows to remove all code of `loadComplete` described in the answer. – Oleg Apr 11 '16 at 07:52
  • @JadChahine: See **UPDATED** part of [the answer](http://stackoverflow.com/a/16383288/315935) and [this one](http://stackoverflow.com/a/7689042/315935) about `forceClientSorting: true` and [the answer](http://stackoverflow.com/a/3772720/315935) and [this one](http://stackoverflow.com/a/5398136/315935) about `fromServer: true` – Oleg Apr 11 '16 at 07:59
  • @Oleg : thanks for the answer, one more question please. Can I make a button that have the same functionality of the **next** or **back** button of the jqgrid? – Jad Chahine Apr 18 '16 at 07:01
  • @JadChahine: Why not? One can get the current page number using `.jqGrid("getGridParam", "page")`, increase or decrease it and call `reloadGrid` with additional parameter `{page: newPageValue}`. – Oleg Apr 18 '16 at 07:09
  • @Oleg : Thanks, I use `var nextPg = $("#myGridId").getGridParam("page") + 1;` to get the next page number then I reload the grid using `$("#myGridId").trigger("reloadGrid",[{page:nextPg}]);` But the jqgrid won't reload. – Jad Chahine Apr 18 '16 at 09:00
  • @JadChahine: You you get the page from one grid `$("#propertiesGrid")` but reload another one `$("#mygridId")`? I tried to use var `nextPg = $grid.jqGrid("getGridParam", "page") + 1; $grid.trigger("reloadGrid", [{page:nextPg}]);` and it works at me like expected. You should probably post new question with detailed description and URL which can one use to debug your code. – Oleg Apr 18 '16 at 09:04
  • No for sure I put the same grid Id, it was a typing problem here. It still not working. – Jad Chahine Apr 18 '16 at 09:08
  • @JadChahine: I can repeat, that it works in my tests. Thus if you want that I help you then you should post the demo, which reproduces the problem which you reports. It could be that you use the code in wrong context (where exactly you use the code fragment). I can't guess what you do. – Oleg Apr 18 '16 at 09:14