I'm setting up an autocomplete form in which I need that every keyword/term is matched.
I'm using an ajax callback to get my results list and I tried many workarounds to convert json results to a autocomplete-capable array but I couldn't get it to work.
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: 'autocomplete.php',
data: request.term,
dataType: "json",
success: function (data) {
var dataArray = [];
for(var i in data)
dataArray.push(data[i]);
var matchArray = dataArray.slice();
var srchTerms = $.trim (request.term).split (/\s+/);
$.each (srchTerms, function (J, term) {
var regX = new RegExp (term, "i");
matchArray = $.map (matchArray, function (item) {
return regX.test (item) ? item : null;
} );
} );
response(matchArray);
},
error: function () {
response([]);
}
});
},
autoFocus: true
});
I think the code itself is working because if I push a normal javascript array it works just fine but as long as I get results from ajax and convert it to a javascript array, it doesn't work anymore.
What I trying to get is that if a given results is equal to "example book title", it should pop up even if my keywords are "example title" or "title book example" etc...