gpt4 book ai didi

javascript - 如何在jqgrid的custom_func中显示自定义警告框

转载 作者:行者123 更新时间:2023-11-29 16:12:15 25 4
gpt4 key购买 nike

我正在使用自定义函数来验证我在编辑时的输入。以下是一段代码。

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, ""];

但这会显示我的警报以及默认警报。有没有办法在返回期间使用仅自定义警报?请建议。提前致谢。

最佳答案

jqGrid 不提供任何直接的方法来自定义验证对话框。尽管如此,还是可以采取一些技巧来实现您的需要。

首先必须检查 jqGrid 如何实现自定义验证。 jqGrid 调用 $.jgrid.checkValues 来验证输入数据。如果需要,方法 $.jgrid.checkValues 调用 custom_func(参见 here)。然后 jqGrid 调用 here $.jgrid.info_dialog 方法来显示错误信息。因此,例如可以将 $.jgrid.info_dialog 方法子类化,以我在 the answer 中描述的方式显示自定义错误消息。 .

我制作了演示该方法的演示。该演示在“名称”列中具有自定义验证。验证要求保存的值必须以文本“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!"];
}
} } },
...
],
...
});

如果尝试保存文本不以“test”文本开头的“Clients”(“name”)列的数据,jqGrid 可能会显示如下所示的错误消息

enter image description here

关于javascript - 如何在jqgrid的custom_func中显示自定义警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24676643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com