【问题标题】:How to display custom alert boxes in custom_func of jqgrid如何在 jqgrid 的 custom_func 中显示自定义警报框
【发布时间】:2014-07-10 12:30:53
【问题描述】:

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

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

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

【问题讨论】:

  • 您使用哪种编辑模式?问题是editrulescustom_funccommon 属性,用于表单编辑、内联编辑和单元格编辑。错误消息将根据编辑模式以不同的方式显示。
  • @Oleg 我用它进行内联编辑。

标签: javascript jquery jquery-ui jqgrid


【解决方案1】:

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 可能会显示如下错误消息

【讨论】:

  • 非常感谢您提供如此详细的解释。这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多