Reputation: 1137
I am using this version for the jQuery autocomplete where I have a list of words :
$(function () {
var availableTags = [
"hello world", "foo bar", "bar foo world", "hello", "bar"
];
function customFilter(array, terms) {
arrayOfTerms = terms.split(" ");
var term = $.map(arrayOfTerms, function (tm) {
return $.ui.autocomplete.escapeRegex(tm);
}).join('|');
var matcher = new RegExp("\\b" + term, "i");
return $.grep(array, function (value) {
return matcher.test(value.label || value.value || value);
});
};
$("#tags").autocomplete({
source: availableTags,
multiple: false,
mustMatch: false
,source: function (request, response) {
// delegate back to autocomplete, but extract the last term
response(customFilter(
availableTags, request.term));
},
});
});
"hello world", "foo bar", "bar foo world", "hello", "bar"
My problem is that I would like to display only "hello world" when typing "world hello" for example.
Also when I type an space, all options are displayed which is incorrect in my case.
How can I achieve this behaviour ?
Thank you
Upvotes: 0
Views: 576
Reputation: 21
forgetting highlighting, you could try iteratively grepping your array for splitted terms
function customFilter(array, terms) {
arrayOfTerms = terms.split(" ");
arrayOfTerms.forEach(function (entry) {
array = $.grep(array, function (e) {
return e.indexOf(entry) >= 0;
})
});
return array;
}
Upvotes: 1