JQuery UI Autocomplete: MinLength In A Textarea, Is There A Way To Begin Counting From 0 In Every New Line?
Solution 1:
I offer this up, using the example I mentioned in my comment.
Working Example: https://jsfiddle.net/Twisty/yfdjyq79/
JavaScript
$(function() {
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 split(val) {
return val.split("\n");
}
function extractLast(term) {
return split(term).pop();
}
$("#tags")
// don't navigate away from the field on tab when selecting an item
.on("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 3,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push("\u2022 " + ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join("\r\n");
return false;
}
});
});
You will want to move your source
portions of course. I updated the split()
and the join()
within select
. Using \r\n
is not required, but I felt it was important to use for a cross platform solution. Not all browsers will use this as the end of line, so I only seek \n
as the delimiter.
UPDATE
Here is an update that will prevent new entries from firing when the term is less then 3 characters.
https://jsfiddle.net/Twisty/yfdjyq79/5/
JS Snippet
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
var resp;
var lastTerm = extractLast(request.term);
if (lastTerm.length >= 3) {
response($.ui.autocomplete.filter(availableTags, lastTerm));
}
},
You can store the value up front but you cannot call $(selector).autocomplete("option", "minLength")
since you're initializing it. That would have been convenient. For example: https://jsfiddle.net/Twisty/yfdjyq79/7/
Post a Comment for "JQuery UI Autocomplete: MinLength In A Textarea, Is There A Way To Begin Counting From 0 In Every New Line?"