Skip to main content

yahoo.autocomplete Example

3 replies [Last post]
sloanb
Offline
Joined: 2003-09-14

I am wanting to use the yahoo.autocomplete widget to lookup names based on what is stored in a database. I see that the widget has a service arg but how do I use this widget to get run the query each time a user types? Any examples? Thanks for the help as always.

I'm still working with the auto-complete and to this point still have not been successful using the service attribute. I have it making the call to a servlet with no problems but apparently the format of the data I'm returning from the servlet is not correct. I'm currently returning a JSON array or object literals just like the value field requires but nothing displays when I start to type.

Updated 12/08/07 - 9:24 CST

After looking more into the source code of the widget I see where my initial problem was. I was using JSON as the return data exchange type when its clearly defined as XML in the widget source. I have been looking at the yahoo docs for this widget and I understand the how the schema works etc.. for the xml schema for this widget is ["item", "title" ]. To me that means 1 of two things for the xml my guesses are below:

1)

Bob the Builder

The Wiggles

2)

I have tried both methods and neither works properly? Any ideas?

Message was edited by: sloanb

Message was edited by: sloanb

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
azawawi
Offline
Joined: 2007-03-17

Hi sloanb,

The XML schema that you need is the following:
[code]


Tennessee


Texas


[/code]

Simply write the following in your page:
[code]

[/code]

And create a file named 'autocomplete.jsp' with the following:

[code]
<%@page contentType="text/xml" pageEncoding="UTF-8"%>
<%
final String query = (request.getParameter("query") != null)
? request.getParameter("query").toLowerCase() : "";
final String[] states = {
"Alabama", "Alaska", "American samoa", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware",
"District of columbia", "Federated states of micronesia",
"Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois",
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Marshall islands", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New hampshire", "New jersey", "New mexico", "New york",
"North carolina", "North dakota", "Northern mariana islands",
"Ohio", "Oklahoma", "Oregon", "Palau", "Pennsylvania",
"Puerto rico", "Rhode island", "South carolina", "South dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virgin islands",
"Virginia", "Washington", "West virginia", "Wisconsin", "Wyoming"
};
out.println("");
for (int i = 0; i < states.length; i++) {
final String state = states[i];
if (state.toLowerCase().startsWith(query)) {
out.println("");
out.println("" + state + "");
out.println("
");
}
}
out.println("
");
%>
[/code]

This will match (insensitively) and display the appropriate US state as your type.

Please let me know if it works for you,
Ahmad

sloanb
Offline
Joined: 2003-09-14

Thank you very much. I will test that here in a few and let you know the results. Been driving me crazy. The schema had me thrown off.

sloanb
Offline
Joined: 2003-09-14

Works like a champ. I appreciate your effort in helping me.