的问题是,你尝试实现双击单元格编辑这不被支持。您当前的代码不工作,因为如果用户按下Tab键,输入或Esc键键nextCell
,prevCell
,saveCell
或restoreCell
会真的叫,但是这些方法测试是否内部参数cellEdit
是true
。
为了展示如何解决它使用下面的代码,我创建the demo问题:
cellsubmit: 'clientArray',
ondblClickRow: function (rowid, iRow, iCol) {
var $this = $(this);
$this.jqGrid('setGridParam', {cellEdit: true});
$this.jqGrid('editCell', iRow, iCol, true);
$this.jqGrid('setGridParam', {cellEdit: false});
},
afterEditCell: function (rowid, cellName, cellValue, iRow) {
var cellDOM = this.rows[iRow], oldKeydown,
$cellInput = $('input, select, textarea', cellDOM),
events = $cellInput.data('events'),
$this = $(this);
if (events && events.keydown && events.keydown.length) {
oldKeydown = events.keydown[0].handler;
$cellInput.unbind('keydown', oldKeydown);
$cellInput.bind('keydown', function (e) {
$this.jqGrid('setGridParam', {cellEdit: true});
oldKeydown.call(this, e);
$this.jqGrid('setGridParam', {cellEdit: false});
});
}
}
修订:如果你要放弃或保存上一次编辑的变化,如果在任何其他小区中的用户点击一个应用下面的扩展代码:
beforeSelectRow: function (rowid, e) {
var $this = $(this),
$td = $(e.target).closest('td'),
$tr = $td.closest('tr'),
iRow = $tr[0].rowIndex,
iCol = $.jgrid.getCellIndex($td);
if (typeof lastRowIndex !== "undefined" && typeof lastColIndex !== "undefined" &&
(iRow !== lastRowIndex || iCol !== lastColIndex)) {
$this.jqGrid('setGridParam', {cellEdit: true});
$this.jqGrid('restoreCell', lastRowIndex, lastColIndex, true);
$this.jqGrid('setGridParam', {cellEdit: false});
$(this.rows[lastRowIndex].cells[lastColIndex])
.removeClass("ui-state-highlight");
}
return true;
}
The new demo示出结果。
更新2:或者,您可以使用focusout
放弃或保存上次编辑更改。见one more demo它使用的代码:
ondblClickRow: function (rowid, iRow, iCol) {
var $this = $(this);
$this.jqGrid('setGridParam', {cellEdit: true});
$this.jqGrid('editCell', iRow, iCol, true);
$this.jqGrid('setGridParam', {cellEdit: false});
},
afterEditCell: function (rowid, cellName, cellValue, iRow, iCol) {
var cellDOM = this.rows[iRow].cells[iCol], oldKeydown,
$cellInput = $('input, select, textarea', cellDOM),
events = $cellInput.data('events'),
$this = $(this);
if (events && events.keydown && events.keydown.length) {
oldKeydown = events.keydown[0].handler;
$cellInput.unbind('keydown', oldKeydown);
$cellInput.bind('keydown', function (e) {
$this.jqGrid('setGridParam', {cellEdit: true});
oldKeydown.call(this, e);
$this.jqGrid('setGridParam', {cellEdit: false});
}).bind('focusout', function (e) {
$this.jqGrid('setGridParam', {cellEdit: true});
$this.jqGrid('restoreCell', iRow, iCol, true);
$this.jqGrid('setGridParam', {cellEdit: false});
$(cellDOM).removeClass("ui-state-highlight");
});
}
}
更新12:用jQuery 1.8酮原料应使用$._data($cellInput[0], 'events');
,而不是$cellInput.data('events')
得到的$cellInput
所有事件的列表。
看起来不错,但是当用户点击活动单元格外部或网格组件外部时,该活动内联编辑器会自动关闭吗? – 2012-03-02 01:19:34
@stackoverflow:这也是可能的。请参阅答案和新演示的“已更新”部分。 – Oleg 2012-03-02 08:10:10
伟大的进步!但是当我单击单元格或行外(例如网格工具栏,网格列标题等)或网格外部时,内嵌编辑器仍处于活动状态并且不会关闭。也可以添加这样的功能吗?谢谢。 – 2012-03-02 12:28:23