16

How to get a cell value in JQGrid?

If I use the following syntax –

var ret = jQuery("#MyGrid").jqGrid('getRowData', id);
ret = ret.ProductId;

it returns the following HTML.

'input class="editable" name=" ProductId " id="0_ ProductId " style="width: 98%;" type="text"'

I actually need the value of the cell.

Thanks. Dev

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
dev
  • 161
  • 1
  • 1
  • 4
  • 1
    possible duplicate of [How to get a jqGrid cell value](http://stackoverflow.com/questions/1775524/how-to-get-a-jqgrid-cell-value) – Dan Jun 24 '10 at 12:02

3 Answers3

26

If you only need the value of a cell that has already been saved, you can get it with this

$('#myTable').jqGrid('getCell',row_id,'column_name');
HomeSlice
  • 269
  • 3
  • 3
  • 1
    be careful with the above solution - From the docs - "Do not use this method when you are editing the row or cell. This will return the cell content and not the actual value of the input element". In other words if you have a formatter applied this will return something like `

    Cell Value Here

    ` [jqGrid Docs](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods)
    – timbrown Apr 18 '14 at 20:32
4

If you try to get the value of a row while it is being edited, you will retrieve markup (as in your example), instead of the actual value. To quote the jqGrid Documentation for getRowData:

Do not use this method when you editing the row or cell. This will return the cell content and not the actual value of the input element

The best way around this is to save your row data before calling getRowData, although alternatively if that is not an option you will have to parse the markup yourself. Which actually is not that hard to do in jQuery, but is still a pain.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Code for what, exactly? Please see the documentation for `saveRow` and `getRowData` for examples of those methods. If you need to parse out the raw HTML contents of `getRowData`, you can use jQuery just like you would handle a regular HTML document. If you are going down this road I recommend using FireBug to understand the format of the raw HTML. Unfortunately I do not have a code example handy because I was able to refactor my code to prevent these kinds of situations. But if you paste the HTML I might be able to help you out... – Justin Ethier Jun 24 '10 at 13:24
  • That's great but how to get the value then? – Csaba Toth Nov 07 '16 at 00:16
  • @CsabaToth - Use `getRowData` or `getCell` and save your data before calling them. – Justin Ethier Nov 07 '16 at 14:51
  • @JustinEthier "before calling them" - you mean before edit? – Csaba Toth Nov 07 '16 at 23:58
0

You have to follow steps :

Read all dataIDs from grid:

var dataIDs = jQuery("#MY_GRID").getDataIDs();

You can iterate on grid based on ID list. Then you will get row using dataID. When your entire row get done then you can get the cell value.

Here is the sample working code

for(i = 0; i < dataIDs.length; i++)
{
var rowData = jQuery("#MY_GRID").jqGrid ('getRowData', dataIDs[i]);

console.log(rowData);
var COLL_1_VALUE = $('#MY_GRID').jqGrid('getCell', dataIDs[i], 'COLL_1');
var COLL_2_VALUE = $('#MY_GRID').jqGrid('getCell', dataIDs[i], 'COLL_2');

console.log(COLL_1_VALUE);
console.log(COLL_2_VALUE);
}
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87