在实现 JQGrid 4.3.0、Jquery 1.6.2 和 JQuery UI 1.8.16 时,我遇到了一个内联编辑问题。当内联编辑被激活时,一些元素会被分配一个自动完成。当取消或保存内联编辑时,自动完成并不总是消失(通过双击选择文本然后点击删除,然后点击退出退出行编辑)。当该行不再处于编辑模式时,将自动完成控件保留在编辑模式。
也许您可以告诉我初始化是否有问题,或者我是否知道“afterrestorefunc”后的事件,字段可以返回到它们的“原始”状态。原始状态显示为 JQGrid 行中的数据。
我尝试在行关闭后删除 DOM,.remove() 和 .empty():
...
"afterrestorefunc": function(){
$('.ui-autocomplete-input').remove(); }
...
但这会导致其他问题,例如 jqgrid 在序列化数据行或编辑时无法找到单元格,需要刷新页面,而不仅仅是 jqgrid,才能再次看到数据从那一行开始。
元素的自动完成功能是在双击该行时创建的:
function CreateCustomSearchElement(value, options, selectiontype) {
...
var el;
el = document.createElement("input");
...
$(el).autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Services/AutoCompleteService.asmx/GetAutoCompleteResponse") %>',
data: "{ 'prefixText': '" + request.term + "', 'contextKey': '" + options.name + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: Trim(item),
value: Trim(item),
searchVal: Trim(item)
}
}))
}
});
},
select: function (e, item) {
//Select is on the event of selection where the value and label have already been determined.
},
minLength: 1,
change: function (event, ui) {
//if the active element was not the search button
//...
}
}).keyup(function (e) {
if (e.keyCode == 8 || e.keyCode == 46) {
//If the user hits backspace or delete, check the value of the textbox before setting the searchValue
//...
}
}).keydown(function (e) {
//if keycode is enter key and there is a value, you need to validate the data through select or change(onblur)
if (e.keyCode == '13' && ($(el).val())) {
return false;
}
if (e.keyCode == '220') { return false }
});
}
其他来源: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing
http://api.jqueryui.com/autocomplete/
更新: 我尝试只在元素聚焦时创建自动完成,并在 onblur 时删除它。那也没有解决问题。似乎只需要触发自动完成下拉菜单。
最佳答案
在调用自动完成之前,我有一个自定义启用/禁用功能。这会导致对同一个 jqgrid 单元格的多个引用调用导致冲突。 因此,在我应该打开内联编辑的行上双击事件期间。获取该行并根据其记录类型分析需要启用/禁用的单元格。它确定关联数组可用的字段,序列化为隐藏字段值,在页面加载期间从后面的代码中获取。
function tableRowDoubleClick(id, iRow, iCol, e) {
...
setCellEditabilityByRecordType(id);
...
}
单元格可编辑性设置方式如下:
function setCellEditabilityByRecordType(id) {
//change some of the fields to read-only
var grid = $('#mygrid');
var i;
var cm;
var cellRecordType = grid.jqGrid('getCell', id, 'RecordType')
//Determine Fields Disabled by evaluation of data from a hidden field.
var disablefields = eval(pg_FieldDisable[cellRecordType]);
for (i = 0; i < disablefields.length; i++) {
cm = grid.jqGrid('getColProp', disablefields[i]);
cm.editable = false;
}
...
}
...
因此,当行的初始设置对双击使用react时,单元格的可编辑性就设置好了。然后触发“CreateCustomSearchElement”。
但是,如果用户双击该行,则会再次触发双击,设置单元格可编辑性,但针对同一行。所以这会导致对单元格的多次引用,在这一点上,我不确定发生了什么。我所知道的是,我必须将行可编辑性集中到一个函数,或者找到一种方法来读取双击的当前行是否真的需要再次设置可编辑性。
引用资料 JavaScript 关联数组:http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/
关于javascript - JQGrid 和 JQuery 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902214/