【问题标题】:Adding a custom column property in SlickGrid在 SlickGrid 中添加自定义列属性
【发布时间】:2014-09-18 15:43:52
【问题描述】:

我想为我在 SlickGrid 中的一些列添加自定义列属性。我有一个自定义编辑器,它使用正则表达式来验证输入。我想在我的列中添加一个正则表达式语句属性,以便我可以为每个列使用相同的编辑器,并从它们列中获取唯一的正则表达式语句。 我想要这样的列:

var columns = [{ id: "id", name: "#", field: "id", cssClass: "cell-title", resizeable: true, sortable: true },
           { id: "upc", name: "UPC", field: "upc", resizable: true, sortable: true, editor: regexValidationEditor, regex: /^[a-zA-Z0-9 ]{0,20}$/ },
           { id: "price", name: "Price", field: "price", resizable: true, sortable: true, editor: regexValidationEditor, regex: /^\d*\.?\d{0,3}$/ }];

如果我可以在我的验证函数中做这样的事情:

function regexValidationEditor(args) {
    var $value;
    var inputBox = "<INPUT class='customInput' type=text />";
    var scope = this;

    this.init = function () {
        $value = $(inputBox)
        .appendTo(args.container);

        scope.focus();
    };

    this.validate = function () {
        if (!args.column.regex.test($value.val())) {
            return { valid: false, msg: "Invalid Data Entry" };
        }
        return { valid: true, msg: null };
    };

    this.init();
}

显然,这是行不通的,但它符合我想要做的事情。

【问题讨论】:

  • 我认为如果验证失败,每列记录都应该包含一条记录描述性消息。
  • 我不确定是否能正确理解您的问题,但您是否正在寻找正则表达式编辑器/验证器?如果那是您正在寻找的东西,那么请查看我对一个非常相似的问题stackoverflow.com/a/9301137 所做的回答......如果不是这样,那么您能否再解释一下。希望有帮助。你总是可以对另一个答案投票,或者我也可以在这里复制答案:)
  • @ghiscoding 就是这样!如果将自定义字段放入列中,我不确定如何访问它们,但我看到您通过 args.column.editorOptions 获得了它们。

标签: javascript regex properties slickgrid


【解决方案1】:

列信息就像您定义它一样进入,因此自定义属性将在那里。提供所有必要的editor functions,验证将起作用。

Fiddle

function Editor(args) {
  var $input, defaultValue;
  var scope = this;

  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.validate = function () {
    if (!args.column.regex.test($input.val())) {
         
        return {
            valid: false,
            msg: "Invalid Data Entry"
        };
    }
    return {
        valid: true,
        msg: null
    };
  };

  this.applyValue = function (item, state) {
    item[args.column.field] = state;
  };

  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.isValueChanged = function () {
    return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
 };

 scope.init();
}

【讨论】:

  • 就是这样!我必须通过 args.column 才能获得该属性。
猜你喜欢
  • 2011-06-09
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多