I am using jQuery autocomplete to receive specific data via Ajax call. The data I receive from Ajax call is a JSON object which contain a JSON array in it. I need to show each result of this json array into autocomplete search results.The format would be something like this :
{
"returnCode":"success",
"status":"success",
"searchResults" :[{"val":"123"},{"val":"456"},{"val":"789"}]
}
Now I need to show 123 456 and 789 in autocomplete dropdown. Tried some code in formatItem and formatResult, but doesn't seem to serve the purpose.
formatItem: function(row,i,max) {
var returnObject = eval("(" + row + ")");
var searchResults = returnObject.searchResults;
if(searchResults.length>0){
for(i=1; i<searchResults.length; i++){
// What to do here?
}
}
Try
var cache = {};
elem.autocomplete({
minLength: 2,
source: function(request, response) {
var term = request.term;
if (!!cache[term]) {
response(cache[term][0]);
return
};
cache[term] = [];
$.getJSON("/path/to/json/")
.then(function success(data) {
var res = $.map(data.searchResults, function(val) {
return val.val
});
cache[term].push(res);
response(res)
}, function error(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown) // log `$.ajax` errors
})
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.