6

We have a grid with datatype json.

We have the following custom formatter:

function opsFormatter (cellvalue, options, rowObject){
    '<a title=MA href=javascript:showDialog(' + rowObject[5] + ')>MA<a>' + '&nbsp;&nbsp;';
}

Instead of rowObject[5] is there any object notation where we can specify the actual column name ("account")? Ie: rowObject.account.

This is the grid definition:

$("#grid-pos").jqGrid({
    colNames:['Product', 'Type','Expiry', 'Put Call', 'Strike', 'Account','Long','Short', 'Open Qty', 'LTD', 'Operations'],
    colModel :[
               {name:'product', index:'product', width:75, sortable:false},
               {name:'type', index:'type', width:50, align:'right', sortable:false},
               {name:'expiry', index:'expiry', width:60, align:'right',stype:'select', searchoptions:{dataUrl:'expiry_select.htm'}, sortable:false},
               {name:'putCall', index:'putCall', width:65, sortable:false},
               {name:'strike', index:'strike', sorttype: 'float', width:70, sortable:false},
               {name:'account', index:'account', width:70, sortable:false},
               {name:'long', index:'long', width:55, align:'right', sortable:false},
               {name:'short', index:'short', width:55, align:'right', sortable:false},
               {name: 'openQty', index:'openQty', width:80, align:'center', formatter:closeoutFormatter, sortable:false},
               {name:'LTD', index:'LTD', width:65, align:'right', sortable:false},
               {index:'operations', width:105, title:false, align: 'center', formatter:opsFormatter, sortable:false}
               ],
               pager: '#div-pos-pager',
               caption: 'Positions'
});  

??

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429

2 Answers2

11

It seems for me absolute correct behavior. The value of the parameter rowObject is object having properties with the same names as defined in the 'name' property of the colModel. The property account is one from there. I suppose that the misunderstanding come from the following part of the documentation of the custom formatter:

rowObject - is a row data represented in the format determined from datatype option. ... If we have datatype: json/jsonstring - the rowObject is array, provided according to the rules from jsonReader

Probably the word array follows to misunderstanding. In JavaScript rowObject.account can be used as rowObject["account"], but one can't use rowObject[5] to access the account property of rowObject. It is just not clear written sentence in the documentation. If you are a native English speaker you can reformulate the text so that it will has no misunderstandings. The documentation is wiki and any person can change any text.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    What I was trying to say is that `rowObject[5]` DOES work but `rowObject["account"]` and `rowObject.account` to NOT work. What I'd like is to be able to use those last two variants.. – Marcus Leon Oct 27 '10 at 22:40
  • Is it because we're using `loadOnce:true`? Maybe this causes an issue as this setting causes the grid datatype to be `local`.. the documentation mention something about object notation only being available for the JSON datatype.. – Marcus Leon Oct 27 '10 at 22:41
  • @Marcus: In my examples I can use notation like `rowObject.account`. You should post the test JSON data and more full `jqGrid` code which can be used to reproduce your problem. – Oleg Oct 27 '10 at 23:03
  • Oleg is there an example you have somewhere which I could look at? – Marcus Leon Nov 03 '10 at 17:07
  • 2
    @Marcus: yes, of cause. Look at http://www.ok-soft-gmbh.com/jqGrid/jsonfromsvc1.htm for example. But I know already the reason **why** we have different results. All depend on the format in which you post the data. If you use XML format of JSON with `repeatitems: false` every row the data has properties and `rowObject` should be indexed by names. If you post compact data as array of strings (for the standard jsonReader) then the `rowObject is the array of the current row and you should use [int] to access the property. – Oleg Nov 03 '10 at 17:29
  • 1
    @Marcus: You can write a small function which get index of `colModel` which convert name to the index. See https://github.com/tonytomov/jqGrid/blob/master/js/grid.base.js#L2933 as an example. The code fragment convert `colname` to `pos`. – Oleg Nov 03 '10 at 17:31
  • Thanks Oleg, makes sense. We do use the compact data as an array of strings.. I will try out your idea for the convert function, good idea! – Marcus Leon Nov 03 '10 at 17:42
  • For that function, how would I modify `$($t.p.colModel)` to refer to `mygrid`? – Marcus Leon Nov 03 '10 at 17:46
  • 1
    @Marcus: Yes in programs which I write I try to make data, which will be send between the server and clients, more compact. So I use always JSON data as array of strings. In other cases I use jqGrid to get show local XML log file in more readable form. In the case the data has named property. – Oleg Nov 03 '10 at 17:47
  • 2
    @Marcus: `$t.p.colModel` is the same as `$("#grid-pos")[0].p.colModel` or probably `mygrid[0].p.colModel` in your case. It is of cause better to save this in a local variable to access it more quickly inside the loop. `var cm = myGrid.getGridParam("colModel");` is another way to get `mygrid[0].p.colModel` – Oleg Nov 03 '10 at 17:53
0

You can get with this code for second index

rowObject.childNodes[2].textContent
Erdi Gürbüz
  • 74
  • 2
  • 3