防止 jqGrid 使用 beforeCellEdit 事件进入编辑单元格

Prevent jqGrid from entering edit cell using beforeCellEdit event?

本文关键字:编辑 单元格 事件 jqGrid 使用 beforeCellEdit 防止      更新时间:2023-09-26

在CellEdit之前捕获事件时,是否可以以某种方式阻止单元格进入编辑模式?

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 });
 }

事件将触发,但列属性的设置似乎不会更改编辑模式。

方法

beforeEditCell将在编辑过程中调用。它的存在主要是为了在新创建的输入或选择元素中进行一些初始化。

如果您需要阻止某些单元格在单元格编辑

模式下进行编辑,我建议您有时在单元格中设置"not-editable-cell"类(例如在cellattrloadComplete中),或者使用beforeSelectRow返回某些单元格的false。它beforeSelectRow返回 false 单元格将不会编辑。

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
    }
}

是的,您需要找到另一种替代方法,因为beforeEditCell只提供了一种在编辑单元格之前执行更多代码的方法。从grid.celledit.js

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

但是,似乎您应该能够调用restoreCell来阻止编辑模式:

恢复单元格

iRow, iCol

恢复索引列iCol中具有行索引iRow(不要与rowid混合)的单元格的编辑内容

例如:

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

如果这不起作用,您可以尝试将此代码添加到 afterEditCell 事件中,这可能是更合适的位置。