在 jqgrid 中验证失败时突出显示错误单元格或输入 [英] highlight error cell or input when validation fails in jqgrid
问题描述
我正在使用 jqgrid 内联编辑并使用编辑规则在网格中进行验证.我想为验证失败的输入添加类以突出显示错误(例如:ui-state-error).我可以使用此设置类以突出显示错误
I am using jqgrid inline editing with validation in grid using edit rules . i want to add class to highlight errors(eg: ui-state-error) for the input which fails in validation . i can set class to highlight error using this
jQuery('#'+grid_id).jqGrid('setCell',row_id,errfields[a],'','ui-state-error',{color: 'blue'});我>
但是当内置验证失败时,它在 jqgrid 中不起作用.如何突出显示验证错误触发的单元格/输入.
But it is not working in jqgrid when inbuilt validation fails . How do i highlight the validation error triggered cell/input .
推荐答案
演示 展示了如何解决问题:
The demo shows how the probelm can be solved:
在演示中,金额"、税收"和总计"列将使用以下验证规则进行验证:
In the demo the columns "Amount", "Tax" and "Total" will be validated with the following validation rule:
editrules:{required:true,number:true}
在出现任何验证错误时,将添加验证失败的第一个输入字段以及其他类ui-state-error".它是标准的 jQuery UI CSS 类.此外,我将焦点设置到输入字段.
On any validation error the first input field where the validation failed dditional class "ui-state-error" will be added. It is the standard jQuery UI CSS class. Addionally I set focus to the input field.
对于实现,我覆盖(链)方法 $.jgrid.checkValues
和 $.jgrid.hideModal
的默认实现.下面是对应的代码:
For the implementation I overwride (chain) the default implementation of the methods $.jgrid.checkValues
and $.jgrid.hideModal
. Here is the corresponding code:
var grid = $("#list");
grid.jqGrid({
// define all jqGrid options
});
var originalCheckValues = $.jgrid.checkValues,
originalHideModal = $.jgrid.hideModal,
iColWithError = 0;
$.jgrid.checkValues = function(val, valref,g, customobject, nam) {
var tr,td,
ret = originalCheckValues.call(this,val, valref,g, customobject, nam);
if (!ret[0]) {
tr = g.rows.namedItem(editingRowId);
if (tr) {
$(tr).children('td').children('input.editable[type="text"]').removeClass("ui-state-error");
iColWithError = valref; // save to set later the focus
//error_td_input_selector = 'tr#'+editingRowId+' > td:nth-child('+(valref+1)+') > input.editable[type="text"]:first';
td = tr.cells[valref];
if (td) {
$(td).find('input.editable[type="text"]').addClass("ui-state-error");
}
}
}
return ret;
};
$.jgrid.hideModal = function (selector,o) {
var input, oldOnClose, td,
tr = grid[0].rows.namedItem(editingRowId);
if (tr) {
td = tr.cells[iColWithError];
if (td) {
input = $(td).children('input.editable[type="text"]:first');
if (input.length > 0) {
oldOnClose = o.onClose;
o.onClose = function(s) {
if ($.isFunction(oldOnClose)) {
oldOnClose.call(s);
}
setTimeout(function(){
input.focus();
},100);
};
}
}
}
originalHideModal.call(this,selector,o);
};
这篇关于在 jqgrid 中验证失败时突出显示错误单元格或输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!