JQGrid:根据内容动态设置单元格不可编辑

问题描述:

我遇到一些问题,即使将列设置为可编辑,也会使某些单元格(使用cellEdit:true)不可编辑。

I'm having some issues getting some cells (with cell true) to be non-editable even though the column is set to editable.

我尝试了很多方法,比如beforeEditCell,格式化程序等。似乎无效。

I've tried many ways, like beforeEditCell, formatters, etc. None seem to work.

我最接近的是将格式化程序设置为我想要编辑的列,然后使用setCell设置'not-editable-cell'类(下面的代码段)。第一次单击单元格时,它很可能进入编辑模式,但是如果您单击其他位置并尝试重新编辑单元格,则它已成功无法编辑。

The closest I've got is by setting a formatter to the column that I'd like to be editable and then using setCell to set the 'not-editable-cell' class (snippet below). The first time you click the cell, it unfortunately goes into edit mode, but if you click elsewhere and try to re-edit the cell, it's successfully uneditable.

I'我还尝试使用相同的剪切但在beforeEditCell内部,它成功地阻止了单元格被编辑,但反过来冻结网格。您不能再选择任何其他单元格。

I've also tried using the same snipped but inside of beforeEditCell, it successfully stops the cell from being edited but in turn 'freezes' the grid. You can no longer select any other cell.

function noEditFormatter(cellValue, options, rowObject) {
    if (cellValue == 'test')
        jQuery("#grid").jqGrid('setCell', options.rowId, 'ColName', '', 'not-editable-cell');
    return cellValue;
}

我们非常感谢任何帮助。

Any help would be much appreciated.

使用 setCell 将类'not-editable-cell'添加到应该不可编辑的单元格的方法是正确的。你只选择了错误的地方。在自定义格式化器内部,网格可能尚未构建到最后。我建议你使用 loadComplete gridComplete 检查当前页面的网格包含,并将某些单元格标记为不可编辑。

The idea to use setCell method to add class 'not-editable-cell' to the cells which should be not-editable is correct. You choose only the wrong place to do this. Inside of custom formatter, the grid can be not yet built till the end. I recommend you to use loadComplete or gridComplete to examine the grid contain of the current page and mark some cells as not-editable.

我准备了示例来证明这一点。与您的示例中一样,所有具有测试文本的单元格都标记为不可编辑。在这种方式中,您可以检查一个单元格并将另一个单元格标记为不可编辑。

I prepared an example which demonstrate this. Like in your example all cells having "test" text are marked as non-editable. In the way you can examine one cells and mark another cells as non-editable.