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

时间:2014-07-10 12:30:53

标签: 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, "" ];
        }
          }

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

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

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

 // where the function param means showCustomBox(type, message, heading);
   return[false, ""];

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

1 个答案:

答案 0 :(得分:6)

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!"];
                }
            } } },
        ...
    ],
    ...
});

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

enter image description here