jqgrid获取字段_jqGrid-字段验证

博客介绍了如何使用jqGrid在行内编辑时实现自定义验证,确保用户在Field1和Field2中输入的数据不同时存在。通过定制验证函数myCustomCheck,保存每个字段的值并在验证Field2时检查两者,实现了防止同时输入的限制。这种方法适用于需要防止特定字段重复输入的场景。
摘要由CSDN通过智能技术生成

I am using jqGrid for inline editing and also when creating new record with the Add button.

For sake of simplicity, say I have 2 fields

Field1 Field2

I need the following rules

If user doesn't enter anything in Field1 or Field2, no validation if needed

If user does enter data, they can enter it in either Field1 or Field2 but not both

解决方案

The validation possibilities of jqGrid has many restrictions especially validation during inline editing. The method $.jgrid.checkValues, which implements the validation will be called (see the line of source code), will be called directly during reading of the corresponding input field. So one have no information about other fields as the current validating.

As the workaround you can save the value from the Field1 during the field validation. The validation of the Filed2 can do the validation of the both fields. The custom validation is the way which you can use in the case.

var field1, field2,

myCustomCheck = function (value, colname) {

if (colname === "field1") {

field1 = value;

} else if (colname === "field2") {

field2 = value;

}

if (field1 !== undefined && field2 !== undefined) {

// validate the fields here

return [false, "some error text"];

} else {

return [true];

}

};

$("#grid").jqGrid({

...

colModel: [

...

{name: 'field1', editable: true, ...,

editrules: {custom: true, custom_func: myCustomCheck}},

...

{name: 'field2', editable: true, ...,

editrules: {custom: true, custom_func: myCustomCheck}},

...

]

...

});

You should don't forget to reset field1 and field2 variables to undefined value either before or after editing (in oneditfunc, aftersavefunc or some other callback).

I used in the above code the "symmetric" version of field1 and field2 validation to make the code working in case of changed order of the fields, which can be important if you use columnChooser. In the case you can't be sure that field1 will be always validated before the field2.

Some additional effects you can archive by usage of "subclassing" of existing jqGrid methods. See the answer as an example.

UPDATED: The demo demonstrate the above idea of the validation more detailed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值