0

I have a grid using jqgrid. I need to store the some data returned by the url:, in a local variable so that I can display it in the subgrid. For this I add that column as a hidden column in the main grid, which is of the format in json:

"Details":[{"Name":"ttt", "Quantity":"12"}] . 

Then in the loadcomplete: function I save the value to a variable using

var griddata = $("#grid').getCol('Details', false);

Now when I access griddata[0], I get a object Object. I tried to parse it to get the values correctly, but to no avail. I tried:

griddata[0].Details.Name 

or

griddata[0].Details.0.Name 

or

griddata[0].Name, 

but all failed. I guess I am missing the format if the data returned by the getcol() function.I looked up the documentation on the method and it says that it returns just the values when we specify false, but could not get any examples.
If there are any examples or if there are any pointer to the solution, it will be greatly appreciated.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Alice
  • 95
  • 4
  • 12

1 Answers1

1

If you check the type of griddata[0] (for example with typeof operator) you will see that it has type of string - this is because the value you are getting is the result of toString() on the object you have passed. The reason for that is the way in which jqGrid is treating values.

If you want to store JSON as the value, you need to stringify it entirely:

"Details": "[{\"Name\":\"ttt\", \"Quantity\":\"12\"}]"

Now you can deserialize those string values into objects like this:

var griddata = $.map($("#grid").jqGrid('getCol', 'Details', false), function(value) { 
    return JSON.parse(value); 
});

After that you can access the objects in the way you want:

var name = griddata[0].Name;
var quantity = griddata[0].Quantity;

As an alternative you can split your object into two columns, so the values can be accessed directly with getCol.

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • Thanks tpeczek. Sorry I am a bit confused. I apologize as I am new to javascript/jquery. I get this value from the jqgrid when I specify the URL where I get the value from. I also get the column values for Details there only. So before I call a getCol function, I am not sure where I can Stringify it. – Alice Oct 11 '12 at 13:14
  • If it's a string, why is console.log saying `[Object object]`? – Barmar Oct 11 '12 at 15:03
  • I checked again just now on the console. This is what I get when I check the value for 'griddata[0] : "[object Object]"' – Alice Oct 11 '12 at 16:19
  • @Barmar Because this is the result of toString() called on an object (the actual value is the string "[Object object]"). You can take a look at this very simple jsFiddle which shows how it works --> http://jsfiddle.net/tpeczek/UcGqg/1/. – tpeczek Oct 11 '12 at 16:39
  • @AshaNair You need to provide this string value on server side - in simple words the value for this cell must be a string, not object when you create response in your server. I might be able to provide more details if I could see your server side code. – tpeczek Oct 11 '12 at 16:42
  • @tpeczek, thank you so much for the input. At this point, I cannot make any changes on the server side as I need to get this out by today. I will stick with the individual column approach for now. For the future, I will be sure to stringify my response. Do you know of any links or guidelines or examples where the json response is stringfied? Or is it done on a case by case basis? Thanks again. – Alice Oct 11 '12 at 18:42
  • I tried to '"Details": "[{\"Name\":\"ttt\", \"Quantity\":\"12\"}]"' in the JSONLint, but it shows that its not a valid json. I am confused now. When I remove the escape character, it works fine. How can I have the server send invalid json data. – Alice Oct 11 '12 at 20:09
  • @AshaNair I have just validated `{"Test": "[{\"Name\":\"ttt\",\"Quantity\":\"12\"}]" }` in online [JSONLint](http://jsonlint.com/) validator and it claims it to be valid JSON (I haven't added any other properties as I don't know which other properties your object contains). – tpeczek Oct 11 '12 at 20:34
  • @AshaNair Please remeber to validate complete JSON, not single unenclosed property. – tpeczek Oct 11 '12 at 21:37