JqGrid JSON designation on objects

there!
I have one column in my jqGrid that is empty.
But I checked the object on the chrome console, and that is 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 check object

 rowdata.books[0].nome "HeadFirst" 

Does anyone know where theres is a trick?

Tks!

+1
source share
2 answers

You should use the colModel property as the name colModel only names that can be used as the property name in JavaScript and as CSS identifier names. Therefore, using name:'books[0].nome' not a good idea.

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

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

In more complex cases, you can use functions as a jsonmap value. for instance

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

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

+1
source

name is for a unique name for a string, not a reference to a JSON object. From the jQGrid colModel documentation :

Specify a unique name in the table for the column. This property is required. Like other words used as property / event names, reserved words (which cannot be used for names) include subgrid, cb, and rn.

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

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

In any case, in order to return to your question, I think you're lucky by passing the name of the book directly - as strings, not objects - and referring to it by name somehow like bookName .

0
source

Source: https://habr.com/ru/post/971219/


All Articles