Search code examples
jsonjqgridnotation

jqGrid JSON notation on objects


there!
I´ve one column in my jqGrid that is empty.
But i checked the object on chrome console and thats fine.

colModel definition

colModel:[
    {name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10},hidden:true},
    {name:'firstName',index:'firstName', width:100,searchoptions: { sopt: ['eq', 'ne', 'cn']}},
    {name:'lastName',index:'lastName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
    {name:'books[0].nome',index:'books[0].nome', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
    {"formatter":"myfunction", formatoptions:{baseLinkUrl:'/demo/{firstName}|view-icon'}}
]

JSON response

{
    "total": "10",
    "page": "1",
    "records": "3",
    "rows": [
        {
            "id": 1,
            "firstName": "John",
            "lastName": "Smith",
            "books": [{"nome": "HeadFirst"}]
        },
        {
            "id": 2,
            "firstName": "Jane",
            "lastName": "Adams",
            "books": [{"nome": "DalaiLama"}]
        },
        {
            "id": 35,
            "firstName": "Jeff",
            "lastName": "Mayer",
            "books": [{"nome": "Bobymarley"}]
        }
    ]
}

chrome console inspect object

rowdata.books[0].nome
"HeadFirst"

Any one know where theres are possibles trick?

Tks!


Solution

  • You should use as the value of name property of colModel only the names which can be used as property name in JavaScript and as CSS id names. So the usage of name:'books[0].nome' is not good idea.

    To solve your problem you can use jsonmap. For example you can use dotted name conversion:

    {name: 'nome', jsonmap: 'books.0.nome', ...
    

    In more complex cases you can use functions as the value of jsonmap. For example

    {name: 'nome', jsonmap: function (item) {
            return item.books[0].nome;
        }, ...
    

    You can find some more code examples about the usage of jsonmap in other old answers: here, here, here, here, here.