jqGrid JSON notation on objects

自古美人都是妖i 提交于 2019-11-28 10:52:48

问题


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!


回答1:


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.




回答2:


name is intended to be a unique name for the row, not a reference to a JSON object. From the jqGrid colModel options documentation:

Set the unique name in the grid for the column. This property is required. As well as other words used as property/event names, the reserved words (which cannot be used for names) include subgrid, cb and rn.

You can also observe how .name is used within grid.base.js - for example:

var nm = {},
...
nm = $t.p.colModel[i].name;
...
res[nm] = $.unformat.call($t,this,{rowId:ind.id, colModel:$t.p.colModel[i]},i);

Anyway, to get back to your question I think you will have better luck by passing down the book name directly - as strings and not objects - and referencing it by name as something like bookName.



来源:https://stackoverflow.com/questions/10729173/jqgrid-json-notation-on-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!