Value returned by getcol function in jqgrid

5.7k Views Asked by At

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.

1

There are 1 best solutions below

9
On BEST ANSWER

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.