MinLength in jquery autocomplete multiple values plugin
I am using jquery autocomplete pluguin. Configured to use multiple values.
I am setting the minLength option : 2
The minLength parameter only works for the first words of autorun as multiple values do not work.
How do I configure or what method do I need to override to fix this issue.
source to share
Several remote demos on the jQuery UI website demonstrate this behavior. You just need to add a custom search function:
$('#autocomplete').autocomplete({
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
}
});
See: http://jqueryui.com/demos/autocomplete/#multiple-remote
source to share
I found the Christian version from 2012 to be very helpful, but here's a reworked version that works in 2016 (jQuery UI 1.12.0):
function split(val) {
return val.split(/\s+/);
}
function extractLast(term) {
return split(term).pop();
}
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
const term = extractLast(request.term)
if (term.length < this.options.minLength) {
return false;
}
response($.ui.autocomplete.filter(
data, term));
}
The option minLength
should be provided as usual and then used inside the function source
. See the Demo if more context is needed.
source to share