1

I am using jqGrid but due to my server side technology I cannot call a URL directly to get the data and can only post to the server via a 3rd party broker. My goal is to be able to load data into the Grid via calling my own JavaScript function that calls the server and returns a JSON string.

What I tried: instead of

url:'http://127.0.0.1/products/index.php’,
datatype: 'json',

I have

datastring:init(),
datatype: 'jsonstring',

my init() function does the server-side call. It works fine for the initial page load. However it is never called again e.g. when I jump between pages, or change the number of rows to view. I need it to recall my init function to refresh the data from the database. However it does not ever call my init() function more than once. I assume it thinks this is a hard coded string which never changes.

I tried to call $("#mygrid").trigger("reloadGrid"); manually but it doesn’t seem to work either.

My Grid is:

<script type="text/javascript">
$(function(){
  $("#list").jqGrid({
    datastr:init(),
    datatype: 'jsonstring',
    mtype: 'GET',
    beforeRequest: beforeReq,
    pager: '#pager',
    rowNum:5,
    rowList:[5,10,30],
    sortname: 'invid',
    sortorder: 'desc',
    grouping:true,
    viewrecords: true,
    gridview: true,

  });
});
</script>

I tried calling

$("#list").setGridParam({datatype:'jsonstring', page:1}).trigger('reloadGrid');

I tried what Oleg has suggested here and here but it still does not seem to call my init function again.

Community
  • 1
  • 1
Daveo
  • 19,018
  • 10
  • 48
  • 71

1 Answers1

1

You current code calls init() once before creating the grid. Then it create the grid using the result returned from init().

If you really need to use datatype: 'jsonstring' (and not datatype: "local" with additional data parameter) then you can reset the value of datastr parameter before calling reloadGrid. The corresponding code could be about the following:

$("#list").setGridParam({
    datatype: "jsonstring",
    datastr: init()
}).trigger("reloadGrid", [{page: 1}]);
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • thanks that seems to work. However I am not sure where to put this code. I tried putting it in beforeRequest function (http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events) but it ends up in a infinite loop – Daveo Jun 24 '13 at 09:49
  • @Daveo: You don't posted where you used `$("#list").setGridParam({datatype:'jsonstring', page:1}).trigger('reloadGrid');` before. So it is difficult to answer where one should place it now place. If `init()` function run synchronously the you can just use `setGridParam` inside of `beforeRequest` **without** usage of `reloadGrid`. – Oleg Jun 24 '13 at 09:58
  • Excellent that seems to work I can get data in the Grid. However paging does not change it always says 1 of 1 pages and there is no next page button even though I have 3 pages. my page size = 10 . My JSON is ` "total": "3", "page": "1", "records": "31",` Next time I am in Germany I owe you a beer – Daveo Jun 24 '13 at 11:54
  • @Daveo: You are welcome! You can try to use `datatype` defined as function (see [the documentation](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#function)) if you really need to implement server side paging which is implemented without usage of jQuery.ajax internally. – Oleg Jun 24 '13 at 12:12