我正在设置SlickGrid,需要一个包含autoComplete编辑器的列。 我尝试了修改TextEditor或DateEditor。什么都行不通。
任何人都可以使用TextEditor作为基础给我一个粗略的草图吗?
非常感谢。
答案 0 :(得分:9)
我在slick.editors.js中做了这个 - 可能有一些错误,但应该可以帮助你开始:
$.extend(true, window, {
"Slick": {
"Editors": {
"Auto": AutoCompleteEditor,
"Text": TextEditor,
"Integer": IntegerEditor,
"Date": DateEditor,
"YesNoSelect": YesNoSelectEditor,
"Checkbox": CheckboxEditor,
"PercentComplete": PercentCompleteEditor,
"LongText": LongTextEditor
}
}
});
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function AutoCompleteEditor(args) {
var $input;
var defaultValue;
var scope = this;
var calendarOpen = false;
this.init = function () {
$input = $("<INPUT id='tags' class='editor-text' />");
$input.appendTo(args.container);
$input.focus().select();
$input.autocomplete({
source: availableTags
});
};
this.destroy = function () {
$input.autocomplete("destroy");
};
this.focus = function () {
$input.focus();
};
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 () {
return {
valid: true,
msg: null
};
};
this.init();
}
如果有帮助,请告诉我。
答案 1 :(得分:5)
只是对上一个答案的一点补充。要使箭头键正常工作,您可能需要执行stopPropagation
。以下是编辑器init
方法的摘录。它是在CoffeeScript中,但你会明白这一点:
$input.bind "keydown.nav", (e) ->
e.stopPropagation() if (e.keyCode == $.ui.keyCode.DOWN || e.keyCode == $.ui.keyCode.UP || e.keyCode == $.ui.keyCode.ENTER) && $('ul.ui-autocomplete').is(':visible')
e.stopImmediatePropagation() if e.keyCode == $.ui.keyCode.LEFT || e.keyCode == $.ui.keyCode.RIGHT
上述代码段的常规JQuery翻译:
$input.bind("keydown.nav", function(e) {
if ((e.keyCode == $.ui.keyCode.DOWN || e.keyCode == $.ui.keyCode.UP ||
e.keyCode == $.ui.keyCode.ENTER)
&& $('ul.ui-autocomplete').is(':visible')) e.stopPropagation();
if (e.keyCode == $.ui.keyCode.LEFT || e.keyCode == $.ui.keyCode.RIGHT)
e.stopImmediatePropagation();
});
答案 2 :(得分:0)
为了完整性以及与ganeshk和arkadiy的答案:
一个名为Extazystas的用户在github上包含了ganeshk解决方案:
https://gist.github.com/Extazystas/b64d98f072344ec395fa
要使自动完成工作,您需要在init函数中包含arkadiy片段,如下所示:
$input.focus().select();
// insert from here ------------------------------
$input.bind("keydown.nav", function(e) {
if ((e.keyCode == $.ui.keyCode.DOWN || e.keyCode == $.ui.keyCode.UP ||
e.keyCode == $.ui.keyCode.ENTER)
&& $('ul.ui-autocomplete').is(':visible')) e.stopPropagation();
if (e.keyCode == $.ui.keyCode.LEFT || e.keyCode == $.ui.keyCode.RIGHT)
e.stopImmediatePropagation();
});
// insert to here ------------------------------
$input.autocomplete({
source: availableOptions
});