我正在使用自定义函数来验证编辑时的输入。以下是一段代码。
editrules:{custom: true, custom_func: customValidation}
function customValidation(val, colname) {
if (val.trim() == "") {
return [ false,"name is required " ];
} else {
return [ true, "" ];
}
}
这可以正常工作,并且如果验证为假,它会显示警报。我想显示我的自定义警报框。
我尝试使用自定义提醒框,例如
showCustomBox('ERROR', 'Name is required! ',{title : 'Error: Edit xxx grid'});
// where the function param means showCustomBox(type, message, heading);
return[false, ""];
但这会显示我的警报以及默认警报。有没有办法在退货时使用oly自定义警报?请建议。提前致谢。
jqGrid没有提供任何直接的方式来定制验证对话框。但是,可以做到一些技巧来实现您所需的东西。
首先,必须检查jqGrid如何实现自定义验证。jqGrid调用$.jgrid.checkValues
以验证输入数据。该方法在需要时$.jgrid.checkValues
调用custom_func
(请参阅此处)。然后jqGrid调用here $.jgrid.info_dialog
方法以显示错误消息。因此,例如可以将$.jgrid.info_dialog
方法子类化,以按照我在答案中描述的方式显示自定义错误消息。
我制作了演示该方法的演示。该演示在“名称”列中具有自定义验证。验证要求所保存的值必须以文本“ test”开头。我曾经alert
显示自定义错误消息。以下是我在演示中使用的代码的主要部分:
var useCustomDialog = false, oldInfoDialog = $.jgrid.info_dialog;
...
$.extend($.jgrid,{
info_dialog: function (caption, content, c_b, modalopt) {
if (useCustomDialog) {
// display custom dialog
useCustomDialog = false;
alert("ERROR: " + content);
} else {
return oldInfoDialog.apply (this, arguments);
}
}
});
...
$("#list").jqGrid({
...
colModel: [
{ name: "name", width: 65, editrules: {
custom: true,
custom_func: function (val, nm, valref) {
if (val.slice(0, val.length) === "test") {
return [true];
} else {
useCustomDialog = true; // use custom info_dialog!
return [false, "The name have to start with 'test' text!"];
}
} } },
...
],
...
});
结果,如果有人尝试保存“ Clients”(“ name”)列的数据而不是以“ test”文本开头的数据,则jqGrid可能会显示以下错误消息
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句