自定义功能仅适用于jqGrid中的编辑模式,而不是添加模式

时间:2016-06-27 16:39:26

标签: jqgrid free-jqgrid

我有一个jqGrid自定义函数editrules: { custom: true, custom_func: checkforduplicates, required:true }

但是,我希望此功能仅在添加模式下运行,而不是在编辑模式下运行。这可能吗?

编辑: - 在回答Oleg之后,我将代码更改为以下。但是,警报不会打印。不知道我哪里错了。

colModel: [
            { key: true, name: 'id', editable: false, formatter: 'integer', viewable: false, hidden: true },
            {
                key: false,
                name: 'name',
                editable: true,
                editrules: {
                    required: true,
                    custom: function (options) {
                        // options have the following properties
                        // cmName
                        // cm
                        // iCol
                        // iRow
                        // rowid
                        // mode - "editForm", "addForm", "edit", "add", "cell" and so on
                        // newValue - the value which need be validated
                        // oldValue - the old value
                        // some additional properties depends on the editing mode
                        alert("mode is " + options.mode);
                        if (options.mode === "add") { // "add" for inline editing
                            var grid = $("#grid");

                            var textsLength = grid.jqGrid("getRowData");




                            var textsLength2 = JSON.stringify(textsLength);

                            alert("i am here");

                            var myAttrib = $.map(textsLength,
                                function (item) { return item.name });


                            var count = 0;
                            for (var k in textsLength) {
                                if (textsLength.hasOwnProperty(k)) {
                                    ++count;
                                }
                            }

                            var text, i;


                            for (i = 0; i < count; i++) {
                                text = myAttrib[i];
                                if (value === text) {
                                    return [false, " - Duplicate category name."];
                                }
                            }
                            return [true, ""];
                        }
                        return true;
                    }
                }
            },

2 个答案:

答案 0 :(得分:1)

免费jqGrid支持旧样式custom_func,其中包含选项valuenameiCol以及新样式验证。要使用新样式验证,不需要指定任何custom_func回调,而是使用一个参数将custom定义为calback函数:

editrules: {
    required: true,
    custom: function (options) {
        // options have the following properties
        // cmName
        // cm
        // iCol
        // iRow
        // rowid
        // mode - "editForm", "addForm", "edit", "add", "cell" and so on
        // newValue - the value which need be validated
        // oldValue - the old value
        // some additional properties depends on the editing mode
        if (options.mode === "addForm") { // "add" for inline editing
            // do the validation
        }
        return true;
    }
}

如果验证了添加表单,则mode属性等于"addForm"options.iRow === -1options.oldValue === nulloptions.rowid === "_empty"。建议使用options.mode检测免费jqGrid中的编辑(或搜索模式),因为其他属性的值(iRowoldValuerowid)取决于编辑模式。

答案 1 :(得分:0)

对于4.7版,我使用此方法。用于为表添加数据类的表格。之后,将执行用户验证的特殊操作。

{
 name : "LOGIN", 
 index : "LOGIN", editrules: {
   required:true,
   custom:true,
   custom_func: dublicateUser
 } 

 ...

 {
  closeAfterAdd : true, 
  width : 500, 
  recreateForm : true,
  afterShowForm : function () {
    jQuery("#TblGrid_list_users").addClass('addMode'); 
  }

...

function dublicateUser() {
  var a;
  var login = jQuery('#LOGIN').val();
  var checkMode = jQuery('#TblGrid_list_users').hasClass('addMode');
  jQuery.ajax({
    type: 'POST',
    data: {login:login, mode:checkMode},
    url: 'code/validate_user.php',
    async: false,
    success: function(data) {
      if (data == 'err') {
        a = 1;
      } 
      else {
        a=0;
      }
    }
  });
  if (a==1) {
    return[false,"error"];
  }
 else {
   return[true];
 }
}