I’ve been looking into Google suggests and I’ve been able to reproduce how they do it.

Front End:

The key behind the whole thing is a little something called XMLHttp. It’s a nice little control that lets you request information from a server just like a browser but behind the scenes. Since it’s behind the scenes it doesn’t require a page reload and is very fast because of it. Think of all the things you can do with. You could validate that a user has entered a valid item number when they leave the field before they even press a button. It makes the whole user experience much more interactive and more like a desktop application. To use the autocomplete you simple put the following in your html page.

<SCRIPT src=”/timesheet/js/ac.js”></SCRIPT>

<SCRIPT>InstallAC(document.form, document.form.project, document.form.addbutton, “search”, “en”);</SCRIPT>

Back End:

I’m not 100% sure how google does their thing on the back end but I came up with a really good approximation. The solution I came up with loads a Trie datastructure on server load with the following code:

ArrayList list = PRJ.getAllProjects(); // get projects from db

trie = new Autocomplete(true, list); //load trie

so basically you create an Autocomplete object passing in a list of Strings. It’s loads the trie and it is kept in memory. Tries are very very efficient. If you add the words ‘banana’ and ‘bandage’ to a trie, the ‘ban’ part is only stored once. I figure the 10k works I used takes up about 200k of memory.

Then when you are ready to use the trie(ie. when the user types) you do a lookup like so:

String qu = request.getParameter(“qu”); //get what user typed

out.print(trie.getCompletion(qu)); //do lookup and send response

This looks up the string typed and sends a response to the webbrowser. So if the user types ‘ba’ then the trie lookup would find ‘banana’ and ‘bandage’ and return a formatted response to the web browser.

The next time you enter your time on the online time entry program you’ll see what i’m talking about. If you have any questions about it just let me know.

Advertisements