Move this topic
New Autocomplete and remote datasource
in Using jQuery UI
•
13 years ago
Hi Everyone,
we're trying to implement the new autocomplete (ui 1.8) and get it to work nicely with a variable as datasource but as soon as we try remote request no suggestions drop down anymore. When looking at the console a get request is sent and answered through html with the correct suggestions - but in the entry field the loading icon just keeps spinning and nothing else happens.
So the static version works, but not the remote datasource one. With the "old" autocomplete plugin the remote datasource did work too! But for styling we would like to implement the new one.
We're using zend and currently html request - also tried switching to json, did not do any better.
Any help would be greatly appreciated!
Thanks
Marc
we're trying to implement the new autocomplete (ui 1.8) and get it to work nicely with a variable as datasource but as soon as we try remote request no suggestions drop down anymore. When looking at the console a get request is sent and answered through html with the correct suggestions - but in the entry field the loading icon just keeps spinning and nothing else happens.
So the static version works, but not the remote datasource one. With the "old" autocomplete plugin the remote datasource did work too! But for styling we would like to implement the new one.
We're using zend and currently html request - also tried switching to json, did not do any better.
Any help would be greatly appreciated!
Thanks
Marc
3
Replies(5)
Re: New Autocomplete and remote datasource
13 years ago
Marc,
Could you supply some sample code and maybe a sample of the returned json. Also, ensure that your returned json is valid: http://www.jsonlint.com/
You said the "old" plugin. I know that it used to take the items as a string on separate lines. Are you following the new requirments for the returned json array?
The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.
When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The data itself can be in the same format as the local data described above.
Hth,
Dave
Leave a comment on fontzter's reply
Re: New Autocomplete and remote datasource
13 years ago
Dear Dave,
thanks for your help - the direction we assumed and you also pointed us to was correct. The problem was caused by wrong array format - correct json but still wrong for autocomplete - seems like the autocomplete needs very specific format. Now it works your text makes also more sense ;-)
From what we found it's like this - maybe can help others to understand the requirements better:
We found 2 options to work with different requirements for the returned data format:
Version1:
Autocomplete:
$("#city").autocomplete({
source: "/controller/format/json"});
Data Format:
[{"value":"Test1","label":"Test1"},{"value":"Test2","label":"Test2"}]
Here you can leave out label - like dave pointed out.
Version 2:
Autocomplete:
$("#city").autocomplete({
source: function(request, response) {
$.ajax({
url: "/controller/format/json",
data: {
q: request.term
},
success: function(data) {
response(data.names)
}
})
}}});
Data Format:
["Test1","Test2"]
So thanks again dave and everyone for a great product!
thanks for your help - the direction we assumed and you also pointed us to was correct. The problem was caused by wrong array format - correct json but still wrong for autocomplete - seems like the autocomplete needs very specific format. Now it works your text makes also more sense ;-)
From what we found it's like this - maybe can help others to understand the requirements better:
We found 2 options to work with different requirements for the returned data format:
Version1:
Autocomplete:
$("#city").autocomplete({
source: "/controller/format/json"});
Data Format:
[{"value":"Test1","label":"Test1"},{"value":"Test2","label":"Test2"}]
Here you can leave out label - like dave pointed out.
Version 2:
Autocomplete:
$("#city").autocomplete({
source: function(request, response) {
$.ajax({
url: "/controller/format/json",
data: {
q: request.term
},
success: function(data) {
response(data.names)
}
})
}}});
Data Format:
["Test1","Test2"]
So thanks again dave and everyone for a great product!
Leave a comment on undecided's reply
Re: New Autocomplete and remote datasource
12 years ago
This seems to have gotten me alittle closer, but I'm still having the problem where it doesn't display the drop down and loading icon just sits. It works when the data is local, but not when making request via server.
I've verified via firebug that the json file was retrieved successfully, and verified that what was returned is valid json.
Here is my client code:
And here is the json text being returned:
This is essentially the demo for Custom Data and Display, but instead of using the data locally, it's getting from remote.
What am I missing?
I've verified via firebug that the json file was retrieved successfully, and verified that what was returned is valid json.
Here is my client code:
- $('#provider').autocomplete({
minLength: 2,
source: function(request, response) {
$.getJSON("server.php", {
term: request.term
}),
response
},
focus: function(event, ui) {
$('#provider').val(ui.item.label);
return false;
},
select: function(event, ui) {
$('#provider').val(ui.item.label);
$('#provider-id').val(ui.item.id);
$('#provider-phone').html(ui.item.phone );
$('#provider-state').html(ui.item.stateselected );
$('#provider-location').html(ui.item.city + " - " + ui.item.state );
$('#provider-policy-link').attr( 'href', ui.item.policy );
$('#provider-credentialing-link').attr( 'href', ui.item.credentialing );
$('#provider-icon').attr('src', '../images/' + ui.item.icon);
return false;
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "<br>" + item.city + " - " + item.state + "</a>" )
.appendTo( ul );
};
And here is the json text being returned:
- [
{
"value": "jquery",
"label": "jQuery",
"desc": "the write less, do more, JavaScript library",
"icon": "jquery_32x32.png"
},
{
"value": "jquery-ui",
"label": "jQuery UI",
"desc": "the official user interface library for jQuery",
"icon": "jqueryui_32x32.png"
},
{
"value": "sizzlejs",
"label": "Sizzle JS",
"desc": "a pure-JavaScript CSS selector engine",
"icon": "sizzlejs_32x32.png"
}
]
This is essentially the demo for Custom Data and Display, but instead of using the data locally, it's getting from remote.
What am I missing?
Leave a comment on dan232323's reply
Re: New Autocomplete and remote datasource
12 years ago

Leave a comment on khalsa1's reply
Re: New Autocomplete and remote datasource
12 years ago
can you supply the code you are working with? the above code actually has logic errors which is the reasoning behind it not working.
-- Kevin
-- Kevin
Leave a comment on Kevin B's reply
Re: New Autocomplete and remote datasource
11 years ago
I also had trouble making the autocomplete function work with remote data. The request was fired, data in correct JSON format was served, but no dropdown.
In my test I used a local html/js file and a servlet on my appserver. Finally I was pointed at a cross scripting exception in the Jquery code using Firefly. Ok: the html/js page and the servlet must be served from the same server. As soon as I moved my htmal page to the server, the autocomplete worked.
In my test I used a local html/js file and a servlet on my appserver. Finally I was pointed at a cross scripting exception in the Jquery code using Firefly. Ok: the html/js page and the servlet must be served from the same server. As soon as I moved my htmal page to the server, the autocomplete worked.
Leave a comment on bob_kennedy's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to undecided's question
Statistics
- 5 Replies
- 19785 Views
- 1 Followers