[Nitro] ajax autocomplete example
Kashia Buch
kashia at vfemail.net
Sun Aug 28 11:24:55 EDT 2005
Hi
I played around with the ajax example a bit and stumbled over the
auto_complete function, which is not yet fully useful like this (imho). I
thought, wouldn't it be better, if there was some kind of "search"
mechanism built in, so you can type a few chars into the box, and then
just get the corresponding items from the list.
Well, I came to a solution, which is pretty much simple, but it's also
crap. I wonder if there is a better way to do this.
--- public/js/controls.js
this.options.method = 'get'; (instead of post)
--- src/controller.rb
def country_auto_complete
searchstring = get_ajax_value
countries = %w{Greece England USA Germany Italy France Netherlands}
countries.delete_if {|x| x !~ /#{searchstring}/i }
build do
ul {
countries.each { |country| li country }
}
end
end
def get_ajax_value
request = response.headers["REQUEST_URI"]
/(.*)_auto_complete[^\?]*\?\1=([^&]*)/.match(request)[2]
end
--- public/index.xhtml
<p>
Auto complete example:<br />
<label>Enter a country:</label>
<input autocomplete="off" type="text" id="country" name="country" />
<div id="country_auto_complete" class="auto_complete"> </div>
</p>
autocomplete="off", since Opera (8.02) doesn't seem to respect the
el.autocomplete = "off" from javascript, and the additional name="country"
to distinguish the GET value from other values and get it by that Regex.
Well, while this approach certainly works quite well, as you can see,
parsing the REQUEST_URI is pretty much ewwww. (I wouldn't even know how to
do that stuff with POST...)
What I would like to know, is there a more standard way to do this?
If there isn't, here would be my idea for an api:
def country_auto_complete(ajax_return_values)
...
end
Sadly I'm not good enough to code that myself, would love to hear your
ideas.
Kash
--
Feel the love
http://pinkjuice.com/pics/ruby.png
More information about the Nitro-general
mailing list