kaze
kaze

Reputation: 4359

Prevent jqGrid from entering edit cell using beforeCellEdit event?

Is it possible to somehow stop the cell from entering edit mode, when catching the event beforeCellEdit?

beforeEditCell: function (rowid, cellname, value, irow, icol) {
    if (cellname != 'aSpecificCol')
        return;

    var grid = $("#grid");

    if (aCondition == "something")
        grid.setColProp('aSpecificCol', { editable: false });
    else
        grid.setColProp('aSpecificCol', { editable: true });
 }

The event fires, but the setting of column property seems to not changing the edit mode.

Upvotes: 2

Views: 6826

Answers (2)

Oleg
Oleg

Reputation: 221997

The method beforeEditCell will be called in the middle of the editing process. It exist mostly to make some initialization in the new created input or select element.

If you need prevent some cells from editing in the cell editing mode I cen suggest you either to set "not-editable-cell" class in the cell sometimes before (for example in cellattr or in loadComplete) or use beforeSelectRow to return false for some cells. It beforeSelectRow return false the cell will be not editing.

beforeSelectRow: function (rowid, e) {
    var $td = $(e.target).closest("td"), iCol = $.jgrid.getCellIndex($td[0]);
    if (/* test some rules here */) {
        return false; // prevent selection and so the editing of the cell
    }
}

Upvotes: 7

Justin Ethier
Justin Ethier

Reputation: 134167

Right, you need to find another alternative because beforeEditCell only provides a means to execute more code prior to editing the cell. From grid.celledit.js:

if ($.isFunction($t.p.beforeEditCell)) {
    $t.p.beforeEditCell.call($t, $t.rows[iRow].id,nm,tmp,iRow,iCol);
}

However, it seems that you should be able to call restoreCell to prevent edit mode:

restoreCell

iRow, iCol

restores the edited content of cell with the row index iRow( do not mix with rowid) in index column iCol

For example:

beforeEditCell: function (rowid, cellname, value, irow, icol) {
    var grid = $("#grid");
    if (cellname != 'aSpecificCol') {
        grid.jqGrid("restoreCell", irow, icol);
        return;
    }

If that does not work, you can try adding this code to the afterEditCell event instead, which may be the more appropriate place.

Upvotes: 0

Related Questions