在我看来,最好的方法是编写自己的编辑器,并将其作为另一个新的自定义编辑器添加到
slick.editor.js
中。这个文件也是为此而制作的。我已经实现了正则表达式测试,效果很好。
以下是我的新编辑器,它不仅适用于正则表达式,还适用于各种条件类型,例如选项
min:2
将需要至少 2 个数字,而
minLength:2
将要求输入至少为 2 个字符的字符串等。现在,您真正寻找的就是我的代码定义中的
pattern
类型。
因此,您将不得不在
slick.editor.js
中包含此代码:
ConditionalCellEditor : function(args) {
var $input;
var defaultValue;
var scope = this;
var condObj = null;
this.init = function() {
$input = $("<INPUT type='text' class='editor-text' />")
.appendTo(args.container)
.bind("keydown.nav", function(e) {
if (e.keyCode === $.ui.keyCode.LEFT || e.keyCode === $.ui.keyCode.RIGHT) {
e.stopImmediatePropagation();
}
})
.focus()
.select();
};
this.destroy = function() {
$input.remove();
};
this.focus = function() {
$input.focus();
};
this.getValue = function() {
return $input.val();
};
this.setValue = function(val) {
$input.val(val);
};
this.loadValue = function(item) {
defaultValue = item[args.column.field] || "";
$input.val(defaultValue);
$input[0].defaultValue = defaultValue;
$input.select();
};
this.serializeValue = function() {
return $input.val();
};
this.applyValue = function(item,state) {
item[args.column.field] = state;
};
this.isValueChanged = function() {
return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
};
this.validate = function() {
var condObj = args.column.editorOptions;
var returnMsg = null;
var returnValid = true;
if(typeof condObj.min != 'undefined') {
if( parseFloat($input.val()) < parseFloat(condObj.min) ) {
returnMsg = "Value should not be less then "+condObj.min;
returnValid = false;
}
}
if(typeof condObj.max != 'undefined') {
if( parseFloat($input.val()) > parseFloat(condObj.max) ) {
returnMsg = "Value should not be over "+condObj.max;
returnValid = false;
}
}
if(typeof condObj.minLength != 'undefined') {
if($input.val().length < condObj.minLength) {
returnMsg = "Value length should not be less then "+condObj.minLength;
returnValid = false;
}
}
if(typeof condObj.maxLength != 'undefined') {
if($input.val().length > condObj.maxLength) {
returnMsg = "Value length should not be over "+condObj.maxLength;
returnValid = false;
}
}
if(typeof condObj.pattern != 'undefined') {
if( condObj.pattern.test($input.val()) != true ) {
returnMsg = (condObj.msg) ? condObj.msg : "Value is invalid following a regular expression pattern";
returnValid = false;
}
}
if(typeof condObj.required != 'undefined') {
if($input.val().length == "" && condObj.required) {
returnMsg = "Required field, please provide a value";
returnValid = false;
}
}
return {
valid: returnValid,
msg: returnMsg
}
};
this.init();
},
在我的SlickGrid列定义内,我调用了新的编辑器并传递一些选项。我决定将这些选项作为对象传递给editorOptions
,这样就可以更灵活地添加任何选项,例如pattern、msg、minLength等等。下面是一个验证电子邮件正则表达式模式的示例。
columns1 = [
...
{id:"email", field:"email", name:"Em@il", width:145, editor:ConditionalCellEditor, editorOptions:{pattern:/^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/, msg:"Must be a valid email"} },
...];
看,好像是个魔法一样的效果!!!
我几乎不再使用 editor:TextCellEditor
了,因为我的新的 ConditionalCellEditor
编辑器给了我更多的灵活性。希望能有所帮助,让我知道它的效果如何...
slick.editors.js
中有标准验证器,我需要指定自己的吗?如果不使用validator: requiredFieldValidator
,代码会是什么样子? - Iurii Dziubanargs.column.validator($input.val());
目前仅将值传递到您的requiredFieldValidator
。您的代码仍将类似于validator: requiredFieldValidator
,但这将引用function requiredFieldValidator(value, input)
。您需要首先将正则表达式定义为输入属性。 - Mike Gwiltregex
属性? - Iurii Dziuban