slickgrid使用正则表达式验证列

Iur*_*ban 3 javascript regex slickgrid

有一个带列验证的简单示例:

    function requiredFieldValidator(value) {
      if (value == null || value == undefined || !value.length) {
        return {valid: false, msg: "This is a required field"};
      } else {
        return {valid: true, msg: null};
      }
    }
Run Code Online (Sandbox Code Playgroud)

并且要验证列,只需要放置此选项: validator: requiredFieldValidator

但是如果我需要传递额外的参数 - 正则表达式字符串,我怎么能使用正则表达式函数.

ghi*_*ing 6

在我的视图中,最好的方法是编写自己的编辑器,您将其添加到slick.editor.js另一个新的自定义编辑器中.这个文件也是为此而制作的.我确实实现了正则表达式测试,效果很好.

这是我的新编辑器,它不仅适用于Regex,还适用于各种条件类型,例如,一个选项min:2需要最小数量为2,而a 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;
            }
        }

        // Now let's return our boolean results and message if invalid
        return {
            valid: returnValid,
            msg: returnMsg
        }
                };

                this.init();
},
Run Code Online (Sandbox Code Playgroud)

然后在我的SlickGrid列定义中,我正在调用我定义的新编辑器并传递一些选项,我决定将其editorOptions作为Object 传递给我,这使我可以更灵活地添加任何我想要的选项,模式,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"} },
...];
Run Code Online (Sandbox Code Playgroud)

瞧,像魅力一样!我几乎editor:TextCellEditor不再使用了,因为我的新ConditionalCellEditor编辑器给了我更大的灵活性.希望它有所帮助,让我知道它是怎么回事......