Javascript jqGrid - 仅编辑可编辑列的某些行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2863874/
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 - edit only certain rows for an editable column
提问by Cosmin Marginean
Is it possible to disable editing in jqGridfor certain cells in a column that is marked as editable?
对于标记为可编辑的列中的某些单元格,是否可以禁用jqGrid中的编辑?
From what I've seen, the only options are "all cells are editable" or "no cells are editable". Is there a way to work around this?
从我所看到的,唯一的选项是“所有单元格都是可编辑的”或“没有单元格是可编辑的”。有没有办法解决这个问题?
回答by Oleg
I'll recommend you to use so named "Inline Editing" for row editing. The most advantage of this method, that it is very intuitive and the user. You can see how it works on the demo page http://trirand.com/blog/jqgrid/jqgrid.html. Choose on this demo "Row Editing" and then "Using Events" or "Input types" on the left tree part. With this method you can implement any custom verification whether the selected row should be allowed to be edited or not inside of the event handle onSelectRowor ondblClickRow. If you allow editing, then you call editRowmethod of jqGrid. This method creates input controls for all editable columns and the user can modify the row values in a natural way. The modifications will be saved if the user press "enter" key or canceled on "esc" key.
我会建议您使用如此命名的“内联编辑”进行行编辑。这种方法的最大优点,就是它非常直观且用户使用。您可以在演示页面http://trirand.com/blog/jqgrid/jqgrid.html上看到它是如何工作的。在此演示中选择“行编辑”,然后选择左侧树部分的“使用事件”或“输入类型”。使用此方法,您可以实现任何自定义验证是否应允许在事件句柄onSelectRow或ondblClickRow. 如果允许编辑,则调用editRowjqGrid 的方法。此方法为所有可编辑列创建输入控件,用户可以以自然的方式修改行值。如果用户按“回车”键或取消“
I personally prefer to implement calling of editRowmethod inside of ondblClickRowevent handler. So the user can continue selecting of rows like usual and can use double click for the row editing. The pseudo code will look like folowing:
我个人更喜欢editRow在ondblClickRow事件处理程序内部实现方法调用。因此用户可以像往常一样继续选择行,并可以使用双击进行行编辑。伪代码如下所示:
var lastSel = -1;
var isRowEditable = function (id) {
// implement your criteria here
return true;
};
var grid = jQuery('#list').jqGrid({
// ...
ondblClickRow: function(id, ri, ci) {
if (isRowEditable(id)) {
// edit the row and save it on press "enter" key
grid.jqGrid('editRow',id,true);
}
},
onSelectRow: function(id) {
if (id && id !== lastSel) {
// cancel editing of the previous selected row if it was in editing state.
// jqGrid hold intern savedRow array inside of jqGrid object,
// so it is safe to call restoreRow method with any id parameter
// if jqGrid not in editing state
grid.jqGrid('restoreRow',lastSel);
lastSel = id;
}
},
pager: '#pager'
}).jqGrid('navGrid','#pager',{edit:false});
回答by AJCE
You can do it logically. You must have some criteria for cells that some cells can be editable and some are not.
你可以从逻辑上做到这一点。您必须有一些单元格条件,有些单元格可以编辑,有些单元格不可编辑。
I have implemented it row wise.
我已经按行实施了它。
When you create XML for jqgrid, give some id to each row.
当您为 jqgrid 创建 XML 时,为每一行提供一些 id。
Based on these ids you can make those rows' cells editable or noneditable using jqgrid methods.
根据这些 id,您可以使用 jqgrid 方法使这些行的单元格可编辑或不可编辑。
Below is beforeEditCell method:
下面是 beforeEditCell 方法:
beforeEditCell: function(rowid, cellname, value, iRow, iCol) {
// here identify row based on rowid
// if the row should not be editable than simply make the cells noneditable using
editCell(iRow, iCol, false);
jQuery(gridid).jqGrid("restoreCell",iRow,iCol);
}
You can further implement yourself.
您可以进一步实现自己。
Hope my suggestion would help you. :)
希望我的建议能帮到你。:)

