tags:

views:

579

answers:

2

I have a couple of columns in jqGrid with edittype="select". How can I read the option value of the value currently selected in a particular row?

e.g.: When I provide the following option, how do I get "FE" for FedEx, etc.

editoption: { value: “FE:FedEx; IN:InTime; TN:TNT” } 

getRowData() for the rowId/cellname returns only the text/displayed component of the select.

If I set a "change" data event on the column, the underlying fires change events only on mouse clicks, and not keyboard selects (there's numerous references to generic selects and mouse/keyboard issues).

Bottomline, when a new value is selected, I need to know the option value at the time of the change, and also prior to posting to the server.

A: 

The documentation for getRowData states:

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

Is the row still being edited when you call getRowData()?

Update

Agreed, jqGrid does not handle <select> very well. In my application I actually was able to get around this by not specifying an edit option (meaning, key/value were both "FedEx"); the translation to ID is then done on the server. This is not the right way to code this, but it worked well enough for my needs at the time.

Justin Ethier
no. the call to getRowaData() is made after editing, e.g. when stuffing row data into a json object to send to the server. Its just that there is absolutely no mention in the docs about getting underlying option values for selects, except by binding to change events, which in general, is flaky on <select>
destroyer of evil
Yeah, I agree as well. I think I will revert to mapping the displayed value (which is the registered value) back to the key server side. Downside is that values need to be unique.
destroyer of evil
BTW, the event afterSaveCell(rowid, cellname, value, iRow, iCol) does return the <option> in **value**.
destroyer of evil
Good to know. Did you end up using this approach, then?
Justin Ethier
A: 

I just solved this question by using setting JqGrid unformat option and use the following function for unformatting cell value.

function Unformat_Select(cellvalue, options, cellobject)
{
    var unformatValue = '';

    $.each(options.colModel.editoptions.value, function (k, value)
    {
        if (cellvalue == value)
        {
            unformatValue = k;
        }
    });

    return unformatValue;
}

The above method will be called everytime when grid need cell data like when you call "getRowData" method. However, my function only support key-paired value edit option. You need to change your data like the following pattern.

editoption: 
{
    value:
    {
        FE:'FedEx', 
        IN:'InTime', 
        TN:'TNT'
    }
}

For more information about unformat option, you can see at the following link.

JqGrid Wiki - Custom Formatter

PS. It's possible to modify my function to support client-side dropdownlist value. But I think it's impossible to apply this function for server-side dropdownlist value.

Update

In the latest jqGrid 3.8.1, I just found some bug when user cancel editing row(or programmatically call "restoreRow" method), jqGrid will create label by using key of data (instead of value of data). I create the following function to fix this issue. For use this, you must it as custom formatter function of this column. This function maps cell value to value of list by comparing key or value.

function JqGridInlineEditor_SelectFormatter(cellvalue, options, rowObject)
{
    var temp = '';
    $.each(options.colModel.editoptions.value, function (key, value)
    {
        if (cellvalue == key || cellvalue == value)
        {
            temp = value;
            return false;
        }
    });

    return temp;
}

So, you can send key or value as column data to be rendered by the above custom formatter.

Soul_Master