tags:

views:

7346

answers:

7

How to get a jqGrid cell value when in-line editing (getcell and getRowData returns the cell content and not the actuall value of the input element).

A: 

You can use getCol to get the column values as an array then index into it by the row you are interested in.

var col = $('#grid').jqGrid('getCol', 'Sales', false);

var val = col[row];
tvanfosson
This don't work. getCol returns the same result as getCell and getRowData: the cell content and not the actuall value of the input element...
raouf
+1  A: 

As you stated, according to the jqGrid documentation for getCell and 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

Since neither of these methods will return your data directly, you would have to use them to return the cell content itself and then parse it, perhaps using jQuery. It would be nice if a future version of jqGrid could provide a means to do some of this parsing itself, and/or provide an API to make it more straightforward. But on the other hand is this really a use case that comes up that often?

Alternatively, if you can explain your original problem in more detail there may be other options.

Justin Ethier
There is no problem at all. But I am very surprised that there is no way to get directly a jqgrid cell value without parsing content or calling a user function !...
raouf
Well, without looking at the source code I imagine the reason why is that during inline-edit the grid adds markup to display the edit controls. At this point the getCell / getRowData functions cannot be used as-is because the data in the grid may be changed out from under it. In this case the grid would have to be smart enough to do the parsing itself, that currently user code has to do. Probably the best solution would be to have the grid attempt to do this parsing itself, but provide some user API (event?) to override the grid's code if (for example) you are using controls not yet supported.
Justin Ethier
+1  A: 

Here is an example of basic solution with a user function.

    ondblClickRow: function(rowid) {
        var cont = $('#grid').getCell(rowid, 'MyCol');
        var val = getCellValue(cont);
    }

...

function getCellValue(content) {
    var k1 = content.indexOf(' value=', 0);
    var k2 = content.indexOf(' name=', k1);
    var val = '';
    if (k1 > 0) {
        val = content.substr(k1 + 7, k2 - k1 - 6);
    }
    return val;
}
raouf
if set, the content contains always the value attribute followed by the name attribute.
raouf
This does not work. It does not pulled out the selected value, just the first one?
Dan
A: 

try subscribing to afterEditCell event it will receive (rowid, cellname, value, iRow, iCol) where value is your a new value of your cell

McLovin
A: 

I think an extension of this would get it for you. retrieving-original-row-data-from-jqgrid

David
A: 

I think a better solution than using getCell which as you know returns some html when in edit mode is to use jquery to access the fields directly. The problem with trying to parse like you are doing is that it will only work for input fields (not things like select), and it won't work if you have done some customizations to the input fields. The following will work with inputs and select elements and is only one line of code.

ondblClickRow: function(rowid) {
    var val = $('#' + rowid + '_MyCol').val();
}
bigrockshow
This does not work, and your JQuery selector is wrong, should be something like $('#'+rowid+' [aria-describedby$=_'+'colName'+']'). But this gets the cell, you still cant retrieve the value withouth parsing.
Dan
Works great for me. By "cell" do you mean the <td>? What I am seeing is this will get the value of the edit control, whatever that is, and also assuming that you haven't done any customizations to the input control as stated in the original answer.
bigrockshow
A: 

Basically, you have to save the row before you access the cell contents.

If you do, then you get the value for the cell instead of the markup that comes when the cell is in edit mode.

jQuery.each(selectedRows, function(index, foodId) {
            // save the row on the grid in 'client array', I.E. without a server post
            $("#favoritesTable").saveRow(foodId, false, 'clientArray'); 

            // longhand, get grid row based on the id
            var gridRow = $("#favoritesTable").getRowData(foodId);

            // reference the value from the editable cell
            foodData += foodId + ":" + gridRow['ServingsConsumed'] + ',';
        });
David C