javascript-如何关闭单元格编辑器?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何关闭单元格编辑器?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3196字,纯文字阅读大概需要5分钟。
内容图文
![javascript-如何关闭单元格编辑器?](/upload/InfoBanner/zyjiaocheng/674/373cbe460b3c465ab8b6c0d2a75928ea.jpg)
我想使用jqGrid双击打开一个单元格编辑器,因此我的代码包括以下部分:
ondblClickRow: function(rowid, iRow, iCol, e)
{
jQuery('#jqGrid').setGridParam({cellEdit: true});
jQuery('#jqGrid').editCell(iRow, iCol, true);
jQuery('#jqGrid').setGridParam({cellEdit: false});
}
效果很好,但是当用户在编辑元素之外单击或按ESC,TAB,ENTER等时,我不知道如何(自动)关闭单元格编辑器…
解决方法:
问题是您尝试在不支持的双击上执行单元格编辑.您当前的代码不起作用,因为如果用户按Tab,Enter或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");
});
}
}
更新3:从jQuery 1.8开始,应该使用$._ data($cellInput [0],’events’);而不是$cellInput.data(‘events’)来获取$cellInput的所有事件的列表.
内容总结
以上是互联网集市为您收集整理的javascript-如何关闭单元格编辑器?全部内容,希望文章能够帮你解决javascript-如何关闭单元格编辑器?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。