views:

154

answers:

2

I need to pass userData to jqgrid, but can't find any examples of how to do this. Here's my attempt:

Sent from the server:

{ total: 25,
  page: currentpage,
  userData: {foo: 'bar'},
  rows: myRows }

and in jqgrid:

var data = jQuery("#grid").getGridParam('userData');

How can I send userData and read it from jqgrid?

EDIT: I know my userData is being sent, because I can see it in Fiddler. I think I'm just stuck on how to read it on the client side.

A: 

jqGrid by default accepts JSON in this format.

{"rows":
    [{"foo":"bar"}]
}

and setting up the jqgrid options you would do something like this:

$("#cashflow-sheet").jqGrid({
    url:'bbc-json.html',
    datatype: "json",
        ...
Moin Zaman
Thanks for your post. I actually have jqgrid working fine and can display rows without a problem. I omitted that code. What I don't understand is how to use jqgrid's `userData` feature. This lets you send an array of user data from the server to the grid.
Slack
+3  A: 

In general the usage of userData is pretty simple. jqGrid give you support to send from the server any additional data which will be saved together with the jqGrid data. So if jqGrid parse the data returned from the server it just look for userdata (not for userData!!!) and save is in the internal parameter userData.

{ "total":25,
  "page":1
  "records":107,
  "userdata": {"foo": "bar"},
  "rows": [...] }

Be careful: the default property in the input data must be userdata and NOT userData like you currently have. You can overwrite the default name of input property jsonReader: {userdata: "userData"} or jsonReader: {userdata: "myData"} if you use userData or myData as the property name with your additional data.

One from the standard usage of userData is for displaying of the footer in the jqGrid. You can use the data for any your other propose. In another answer it is shown how to use userData to select the some row/rows directly after the loading data from the server.

If you use loadonce:true parameter, the usage of userData will be a little more tricky because after the first load the data from the parameter userData will be deleted, so you have to save there in the external object.

Of cause you can access the userData with respect of jQuery("#grid").getGridParam('userData') only after the data are loaded. So you should do this inside of loadComplete event handle or later. By the way inside of loadComplete event handle you can access to all data which are send to you from the server through data parameter of loadComplete event. So you can read any other additional data and save there somewhere.

Oleg
Thanks. In case anyone reads this later, the summary is that when talking about a GridParam it's `userData`, otherwise it's `userdata`. Read `userdata` in the `loadComplete` event handler. To set `postData` with `userdata` do this: `loadComplete: function(data){jQuery("#ticketlist").setGridParam({ postData: { foo2: data.userdata.foo} });}`
Slack
@Slack: You can also use `jQuery("#grid").getGridParam('userData')` inside of `loadComplete` like `loadComplete: function(){jQuery("#ticketlist").setGridParam({ postData: { foo2: jQuery("#grid").getGridParam('userData').foo} });}`. It is only important to understand that in the data send from the server one have to place `userdata` and the data will be saved in `userData` parameter of jqGrid. So it can be accessible trough `getGridParam('userData')`. But the most important, that now your program works and can use `userdata`/`userData`. :-)
Oleg
@Oleg man I've been using userData instead of userdata in the json generator and I've been going crazy, thanks alot! +1
Neo
@Neo: You welcome!
Oleg