Short version of the problem
Autocomplete works when the input line matches the result line, but not otherwise. Data is successfully deleted from JSON.
Longer version
I want to dynamically edit the original autocomplete content with JSON data.
The approach below works when the search string matches the first_name and last_name fields.
But the JSON URL returns more, for example. when searching by username, it still returns the correct data. But this is not shown in autocomplete, and my theory is that autocomplete UI causes the "input value" to be the same as the "result value".
JSON data is returned:
[
{"pk": 1012912, "fields": {"first_name": "Barack", "last_name": "Obama"}},
{"pk": 1017519, "fields": {"first_name": "George", "last_name": "Bush"}}
]
Autocomplete code
, .
$('#search').autocomplete({
source: [],
search: function(event, ui){
results = [];
var json = jQuery.getJSON('http://jsondata.com?query='+event.target.value, function(data){
for (var i=0; i<data.length; i++){
results.push(data[i].fields.first_name + ' ' +data[i].fields.last_name);
};
}).success(function(){
$('#search').autocomplete('option', 'source', results);
console.log(results);
});
},
});
" " #search, , .
, "baracko", , JSON ( console.log), .
- , ?
googlers ,
ek_ny JSON:
[{ 'value' : 'Barack Obama'}, { 'value' : 'George Bush'}]
:
$('#search').autocomplete({
source: function(req, res){
jQuery.getJSON('http://jsondata.com/?query='+req.term, req, function(data){
var results = [];
$.each(data, function(i, val){
results.push(val.value)
});
res(results);
});
},
});