【发布时间】:2011-07-20 02:32:07
【问题描述】:
我目前正在使用内联编辑进行编辑,当我在网格外单击时,它仍在编辑中。我应该使用什么事件处理程序来使其调用恢复行函数,以便将数据实际发送到服务器的唯一方法是用户按下回车键。
提前谢谢
【问题讨论】:
标签: jquery jqgrid jqgrid-php
我目前正在使用内联编辑进行编辑,当我在网格外单击时,它仍在编辑中。我应该使用什么事件处理程序来使其调用恢复行函数,以便将数据实际发送到服务器的唯一方法是用户按下回车键。
提前谢谢
【问题讨论】:
标签: jquery jqgrid jqgrid-php
即使我在使用内联编辑时也遇到了同样的问题。我已经寻求解决方法。我仍然不知道这是否是一个正确的解决方案。
当我编辑一行时,我使用了这种东西
var lastSel;
// In grid I am using some thing like this for editing
ondblClickRow: function(rowId, iRow, iCol, e){
//initially I am saving my prev row selected for editing and then editing the selected row.
if(rowId && rowId!==lastSel){ //lastSel is a global variable
jQuery(this).saveRow(lastSel);
lastSel=rowId;
}
$(this).jqGrid('resetSelection');
$(this).jqGrid('editRow', rowId, true, null, null, 'clientArray');
}
当我想将数据发送到服务器进行更新时,我使用第一行中的以下语句,然后将数据发送到服务器。
$(gridId).saveRow(lastSel);//where lastSel is the global variable I have selected.
希望这能让您了解如何解决您的问题。 顺便说一句,我只制作了一行可以在任何时间点进行编辑。
【讨论】:
不管怎样,我已经想出办法了。只是想把它留在网上可能会很好,因为我浪费了很多时间来弄清楚如何去做。希望对你有帮助=)
$(document).click(function(e){
if(e.target.id != lastSelectRoot+"_FieldName"){
jQuery('#grid').jqGrid('restoreRow',lastSelectRoot);
lastSelectRoot = null;
}
});
只需将这段代码添加到某处并将适当的部分更改为您已经使用的部分,例如 FieldName 和 lastSelectRoot 和 #grid。
【讨论】:
我不知道您是如何触发内联版本的。我使用 jqGrid 的 ondblClickRow 事件,并且还在寻找一种方法来在用户离开 input 或 select (edit) 元素时恢复行。
我发现跟踪最后选择的元素并在每次点击其他元素时检查它很麻烦。所以,我认为更方便的方法是将restoreRow 触发器附加到当前正在编辑的input 或select 元素的blur 事件上,如下所示:
ondblClickRow: function(rowid, iRow, iCol, e) {
grid.jqGrid('editRow', rowid, true);
$("input, select", e.target).focus().blur(function() {
grid.jqGrid('restoreRow', rowid);
});
return;
}
这样,只要用户离开编辑字段而不按回车,该行就会恢复。
这种方法对我很有效,希望对其他人也有帮助。
【讨论】:
colModel 是您正在寻找的:editoptions: { dataEvents: [ { type: 'keypress', fn: function(e) { if ((e.keyCode || e.which) == 13) {/* your code here */} } } ] }
由于主要问题是您希望在单击网格外时失去焦点,因此我编写了此函数以在网格没有()单击的元素时取消选择单元格:
$(document).click(function(e) {
e.preventDefault();
//gets the element where the click event happened
var clickedElement = e.target;
//if the click event happened outside the grid
if($("#myGridId").has(clickedElement).size() < 1){
//unselect the grid row
$("#myGridId").jqGrid("editCell", 0, 0, false);
}
});
【讨论】:
length == 0 而不是size() < 1
我尝试了几种不同的变体。基于 Mauricio Reis 的 code 我自己写的。
var lastSel = -1;
$("#mygrid").jqGrid({
...
beforeSelectRow: function(rowid) {
if (rowid !== lastSel) {
lastSel = rowid;
$("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
}
return true;
},
onCellSelect: function(rowId,iCol,cellcontent,e) {
if(iCol == 1 || iCol == 2) // editable columns
sgrid.jqGrid('editRow', rowId, true);
},
...
});
...
$(document).click(function(e) {
if(sgrid.has(e.target).size() < 1)
$("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
});
如果您想保存行而不是取消编辑,只需放置
$("#mygrid").jqGrid('saveRow', lastSel); // save row
改为
$("#mygrid").jqGrid('restoreRow',lastSel); // cancel edit
【讨论】:
此解决方案对我有用,并且看起来比其他选项更简单。是通用的,不需要任何全局变量。
$(document).on('focusout', '[role="gridcell"] *', function() {
$("#mygrid").jqGrid('editCell', 0, 0, false);
});
基于 $(document).on('click') 的解决方案存在潜在缺陷。像 select2 这样的一些组件不会将点击事件传播到文档,所以如果你在页面上有它并且它被点击了(我的情况就是这样),它会失败。
【讨论】: