如何在jqgrid的custom_func中显示自定义警报框

Pop*_*ppy 2 javascript jquery jquery-ui jqgrid

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

editrules:{custom: true, custom_func: customValidation}


function customValidation(val, colname) {
        if (val.trim() == "") {
        return [ false,"name is required " ];
        } else {
        return [ true, "" ];
        }
          }
Run Code Online (Sandbox Code Playgroud)

这工作正常,如果验证为false,它会显示警告.我想显示我的自定义提醒框.

我尝试使用我的自定义提醒框

showCustomBox('ERROR', 'Name is required! ',{title : 'Error: Edit xxx grid'});

 // where the function param means showCustomBox(type, message, heading);
   return[false, ""];
Run Code Online (Sandbox Code Playgroud)

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

Ole*_*leg 6

jqGrid不提供任何直接定制验证对话框的方法.然而,人们可以做一些技巧来实现你需要的东西.

首先,我们必须检查jqGrid如何实现自定义验证.jqGrid调用$.jgrid.checkValues验证输入数据.如果需要,该方法$.jgrid.checkValues调用custom_func(参见此处).然后jqGrid调用 $.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!"];
                }
            } } },
        ...
    ],
    ...
});
Run Code Online (Sandbox Code Playgroud)

结果jqGrid可以显示如下所示的错误消息,如果有人试图保存"客户端"("名称")列的数据,该文本不以"test"文本开头

在此输入图像描述