【发布时间】: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