javascript JQGrid:根据内容将单元格动态设置为不可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4718742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQGrid: Dynamically set a cell to uneditable based on content
提问by Shawn
I'm having some issues getting some cells (with cellEdit: true) to be non-editable even though the column is set to editable.
我在使某些单元格(使用 cellEdit: true)不可编辑时遇到了一些问题,即使该列设置为可编辑。
I've tried many ways, like beforeEditCell, formatters, etc. None seem to work.
我尝试了很多方法,比如 beforeEditCell、格式化程序等。似乎都没有。
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.
我得到的最接近的方法是将格式化程序设置为我希望可编辑的列,然后使用 setCell 设置“不可编辑单元格”类(下面的代码段)。第一次单击单元格时,不幸的是它会进入编辑模式,但是如果您单击别处并尝试重新编辑单元格,它就成功地无法编辑了。
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.
我也尝试使用相同的剪断但在 beforeEditCell 内,它成功地阻止了单元格被编辑,但反过来“冻结”了网格。您不能再选择任何其他单元格。
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.
任何帮助将非常感激。
回答by Oleg
The idea to use setCellmethod 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 loadCompleteor gridCompleteto examine the grid contain of the current pageand mark some cells as not-editable.
使用setCell方法将类“not-editable-cell”添加到不应编辑的单元格的想法是正确的。您只选择了错误的地方来执行此操作。在自定义格式化程序中,直到最后才能构建网格。我建议您使用loadComplete或gridComplete检查当前页面的网格包含并将某些单元格标记为不可编辑。
I prepared an examplewhich 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.
我准备了一个例子来证明这一点。就像在您的示例中一样,所有具有“测试”文本的单元格都标记为不可编辑。通过这种方式,您可以检查一个单元格并将另一个单元格标记为不可编辑。
回答by V? ?ình Toàn
var cellattr = function(rowId, tv, rawObject, cm, rdata) {
if(rawObject.locked) return ' class="not-editable-cell"';
};
};
In colModel: each column options add
在 colModel:每列选项添加
{name: 'name',index: 'name', editable: true, width: 100, sortable: false, align: 'center', cellattr: cellattr}
回答by Martin
I had to solve this now (2015) and found an approach that looks clean: specify a function for cellbegineditthat returns falseif the cell is not allowed to be edited. Taken from the linked article and modified:
我现在必须解决这个问题(2015 年),并找到了一种看起来很干净的方法:如果不允许编辑单元格,则为cellbeginedit该返回指定一个函数false。摘自链接文章并修改:
var checkIfRowIsValid = function (rowIndex) {
//somehow get cellValue
...
if (cellValue == 'test') return false;
}
// initialize jqxGrid
$("#jqxgrid").jqxGrid(
{
source: dataAdapter,
editable: true,
selectionmode: 'singlecell',
columns: [
{ text: 'First Name', columntype: 'textbox', datafield: 'firstname',
width: 90, cellbeginedit: checkIfRowIsValid},
{ text: 'Last Name', datafield: 'lastname', columntype: 'textbox',
width: 90, cellbeginedit: checkIfRowIsValid}
]
});

