Search code examples
jqgrid

jqGrid: is it possible to selectively make cells editable?


how can I make one single cell editable out of a not editable column?
my javaScript looks like this:

$( '#grid' ).jqGrid({
    // ...
    cellEdit : true,
    colModel : [
        { name : "id", index : "id", editable : false },
        { name : "wbs", index : "wbs", editable : false },
        { name : "value", index : "value", editable : false }
    ],
    loadComplete : function(data) {
        // ... foreach ( cell in data.rows.columns ) ...
        if ( cell.shouldBeEditable ) {
            jQuery('#grid').setCell(cell.row, cell.col, '', 'green', { editable : true });
        }
    }
    // ...
}

so, after globally setting columns as not editable, I try to set them as editable locally, based on some criteria (to identify them more easily I also paint them green).

Alas, it's not working: cells become green, but when I try to click them they do not become editable.
Inspecting the selected cell with firebug reveals the edit-cell class to be correctly applied.
As a last note, it's working if I set columns as editable in the first instance.


Solution

  • I found a nice workaround to the problem (thanks to Walter for getting me on the right track).

    Instead of locking all the cells and selectively unlocking them in a declarative manner (which may lead to long load times), I'm declaring all the cells as editable.
    Then I'm attaching a callback to afterEditCell option:

    var $grid = $('#grid');
    $grid.jqGrid({
        // ...
        afterEditCell : function(rowid, cellname, value, iRow, iCol) {
            if ( shouldNotBeEditable(iRow, iCol) ) {
                $grid.setCell(rowid, cellname, '', 'not-editable-cell');
                $grid.restoreCell(iRow, iCol);
            }
        },
        // ...
    });
    

    This way the cell is immediately reset to its previous value and I ensure that the callback gets called only once per not-to-be-edited cell, since the cell is made into a not-editable-cell after the first call.