1

Just want to confirm something here. Is it possible to use jqGrid's userData with a javascript type directly (as opposed to using an object with field(s))?

If I modify the example in the jqGrid doc, I would like to return this json to jqGrid:

{ 
  total: "xxx", 
  page: "yyy", 
  records: "zzz", 
  userdata: 0, 
  rows : [ 
    {id:"1", cell:["cell11", "cell12", "cell13"]}, 
    {id:"2", cell:["cell21", "cell22", "cell23"]}, 
    ... 
  ] 
}

and have jQuery("grid_id").jqGrid('getGridParam', 'userData') return 0.

Presently it seems to return an empty object { } instead.

I use jqGrid 4.4.1.

Evren Kuzucuoglu
  • 3,781
  • 28
  • 51

2 Answers2

1

userdata part of the JSON input for jqGrid should be object. If you need to return scalar inside of userdata you should use something like

"userdata": { mycounter: 0 }

and then use

var userdata = $("#grid_id").jqGrid("getGridParam", "userData");
if (userdata) {
    alert(userdata.mycounter);
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Yep that's the workaround. Strange though, if I return 1 instead of 0 it actually works: so it kinda almost work except for one value... I'll use your way though since it will work for all values. I'll have to create a type specifically in my .net code though, since I use a .net-to-json serializer to build this json. – Evren Kuzucuoglu Jan 31 '13 at 11:44
  • @EvrenKuzucuoglu This is because how jqGrid extracts data from request, exactly this line: `ts.p.userData = $.jgrid.getAccessor(data,dReader.userdata) || {};`. The `||` operator will return left part if it can be converted to `true` otherwise it will return right part. The value `0` in JavaScript evaluates to `false`. – tpeczek Jan 31 '13 at 11:51
  • Yes, so it would do the same with false, null and undefined. Better to use an object then! Thanks. – Evren Kuzucuoglu Jan 31 '13 at 11:53
  • 1
    @EvrenKuzucuoglu: In .NET you can use anonymous types (something like `userdata = new { mycounter = 0 }`). So creating of new type is not really required. In ASP.NET MVC you can returns anonymous type directly. In some cases (ASMX web-services for example) you really need to specify type for return object, but usage of `object` for `userdata` should work too I think. – Oleg Jan 31 '13 at 11:53
  • I wish I would have thought about that earlier during my project! I could have avoided creating plenty of types this way! – Evren Kuzucuoglu Jan 31 '13 at 11:56
0

OK so after a bit of exploration, it seems the problem is with the value 0. Other values (I tried 1) come back fine, but when it's 0 I get {}.

I also tried changing the reader to use a function instead of the name of the field:

jsonReader: {
  ...
  userdata: function(json) { return json.response.userdata; },
  ...
}

But the behaviour is exactly the same: {} when the value is 0, 1 when it's 1.

I also updated to 4.4.4 but it doesn't change it.

I don't know if this qualifies as a bug or a non-documented restriction, but I'll return an object with a numerical field instead of the numerical value itself, and it will solve the issue.

Evren Kuzucuoglu
  • 3,781
  • 28
  • 51