的問題是,你嘗試實現雙擊單元格編輯這不被支持。您當前的代碼不工作,因爲如果用戶按下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