Update on Rails, jQuery, autocomplete
Today I changed my auto_complete_jquery plugin (which I blogged about previously) to work with a different jQuery autocomplete plugin (that's not confusing is it?!). Previously, I'd been using the "jquery-autocomplete" plugin, but had been having problems with it always being case sensitive, and with it being pretty darn slow. To solve that, I wound up switching to Dylan Verheul's jquery autocomplete plugin, which is fantastic! So, I had to update my plugin (literally changed two lines, along with the readme and some comments).
So, what does it all mean? First, things are no longer case-sensitive (although you can tweak that if you need it, see the docs for DV's jquery plugin). Second, the speed is near instant, and if it's taking any time at all, there's now a CSS style you can set to show an indicator while the AJAX call is running (nice!). Further, there are a slew of options you can set, but one of the coolest things is the ability to set a formatter JavaScript function to adjust the display of the returned results, but without affecting the actual value that is placed in your text field. This is really cool for providing further information about a given matched item. For example, I use it to display, on a line below the matched name, the location of the item. There's an interactive example of this on Dylan Verheul's site.I highly recommend updating all around on this. For those that might be using my prior version, the changes you'd need to make are:- Change your JavaScript calls that make a HTML input into an auto-complete from this format:
$("input#post_title").autocomplete({ ajax: "auto_complete_for_post_title" })
to this:$("input#post_title").autocomplete("auto_complete_for_post_title") - Update to the HEAD of my plugin.
- Remove the old
jquery.ui.autocomplete*.jsfiles, and install Dylan's singlejquery.autocomplete.jsfile, updating your JS includes accordingly. Same goes for the stylesheet.
function formatAutocompleteItem(row) { return row[0] + "<br><i>" + row[1] + "</i>";}$("input#post_title").autocomplete("auto_complete_for_post_title", { formatItem:formatAutocompleteItem })