2

Is it possible to start from page=0 in a jqGrid?

I am working with data that is out of my control and when i request page 1 I actually get page 2 from the server, so therefore I am looking for some way to actually request page 0.

As far as i can see, setting the page parameter in the jqGrid options to zero has no effect. When inspecting the request the page parameter is still 1.

I have also tried to change the page parameter later on after initialization. This sets the page to 1:

grid.setGridParam({page:0}).trigger('reloadGrid');

This on the other hand sets page successfully to 5 in the request:

grid.setGridParam({page:5}).trigger('reloadGrid');

I guess there is some check against requesting page 0 but any ideas on how to work around this?

Andreas
  • 73
  • 7

2 Answers2

4

If I understand your problem correct you can solve it by using serializeGridData event handler and page property in the jsonReader defined as function.

I don't tested what you find below, but I hope either the code or a little modified code should work:

serializeGridData: function(postData) {
    if (typeof(ts.p.postData.page) === "number") {
        postData.page--; // decrease the value of page before sending to the server
    }
    return postData;
},
jsonReader: {
    page: function (obj) {
        return obj.page + 1; // increase the value of page returned from the server
    }
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • This works for me! Although I changed typeof(ts.p.postData.page) to typeof(postData.page) since ts was not defined. Wow, since I started to use jqGrid I have read sooooo many posts that you have answered Oleg. Thank you for being so dedicated towards helping people! – Andreas Jul 06 '11 at 08:01
  • @Andreas: You are welcome! Thanks, I removed the `ts.p.` prefix before `postData`. Two years ago I was newcomer in web development, JavaScript, jQuery and jqGrid. Before I did software development since many years, but in other areas. So I decided to answer on the questions about jqGrid after the project where I used jqGrid was finished. I did it mostly to keep the new knowledge and expand it. After some time it become a little like my new hobby. I am glad that I could help many people. Best regards. – Oleg Jul 06 '11 at 09:49
1

It should be something like this:

grid.trigger("reloadGrid",[{page:1}]);

See Oleg's reply to a similar question.

UPDATE:

jqGrid pager starts from 1 and the method reloadGrid checks if the page is < 0, so it seems there's not much you can do.

Community
  • 1
  • 1
LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • Thank you for your answer but the problem is not that i can't request and load pages 1-x but since my server is starting its paging function on page=0 I need to request page=0 for the very first page. – Andreas Jul 05 '11 at 13:32
  • @Andreas: forgive me, but, wouldn't it simpler to manage that on server-side doing something like page = page - 1 ? – LeftyX Jul 05 '11 at 13:51
  • absolutely, that would be the best solution but it is just that i don't have easy access to the server implementation. – Andreas Jul 05 '11 at 14:04
  • @Andreas: I am trying to figure out if there's a way to ovveride the javascript implementation but I guess it's not that easy. – LeftyX Jul 05 '11 at 14:06
  • @Andreas: I've updated my answer. the method reloadGrid in the module grid.base.js checks if you're trying to reload a page < 0 and sets it to 1. Maybe @Oleg has got a better solution than mine. – LeftyX Jul 05 '11 at 14:11