lucasdc
lucasdc

Reputation: 1042

jqGrid - Accessing an array inside a JSON

my JSON looks like:

[{"codDiretor":"123",
  "nomeDiretor":"Nome do Diretor",
  "data":"29/01/2014",
  "documentos":[{"codDocumento":"1", "nomeDocumento":"Primeiro Doc"}, 
                {"codDocumento":"2","nomeDocumento":"Segundo Doc"}]
}]

I have a column on my grid called "documentos" to retrieve the array. I'm trying to access it this way:

var documentos = $("#grid").jqGrid('getCell', dados, 'documentos');

alert(documentos) displays [object Object, object Object], which means it "can see" what is in the array, since it has 2 index and it show 2 Object. How can I access it? I tried: documentos[0].codDocumento and it shows undefined.

Thanks,

Lucas.

Upvotes: 0

Views: 1112

Answers (3)

lucasdc
lucasdc

Reputation: 1042

this is how I solved. I could notice that when the grid was being populated, I was receiving a real Object (and not a String with "Object" in it). So that, I use stringify() to convert Object -> String.

{name:'codDocumento', width:80, sortable: true, editable: false, jsonmap:"documentos",
 formatter: function (cellvalue) {
     if(cellvalue != null) {
         return JSON.stringify(cellvalue);
     }
     else
         return cellvalue;
     }
}

After that, when I want to access it like an array, I convert it from String -> Object.

var codDocumento = $("#grid").jqGrid('getCell', dados, 'codDocumento');
var jsonArray = $.parseJSON( codDocumento);

With this I can access it with: jsonArray[INDEX].codDocumento

Upvotes: 1

lonewolf217
lonewolf217

Reputation: 629

I dont think this is going to work. since console.log(documentos[0]) printed a "[" that shows that the data in that cell is just a text string, not an object so you are unable to work it it as an array or an object to find out the contents. What are you trying to accomplish here by returning an array ? Typically the grid cell always expects a text response of some sort to print in the cell.

Upvotes: 0

Hidde
Hidde

Reputation: 11931

Try the console.log(data); command.

In your browser, open the console (usually F12), and the output will be shown there. It is easier debugging than using alert();.

In Firefox you might need to install FireBug.

Upvotes: 0

Related Questions