September 13, 2006
Cascading selects vs. auto_complete
I have lately come to dislike cascading selects (where the value from one select populates a second select with some values). These beasties have their purpose in life, but for the most part they are unnecessary evils.
If you have a ‘Really Long List’ to select from, then using a cascading select makes a lot of sense (unless you want your users to hate you). The problem is that they are a pain to code (well, not so bad if you use the KRJS plugin ), and they are still a pain to use if you actually know what you are looking for (then you have to remember what ‘category’ if falls in).
Lately I have been getting around this little problem using auto-complete boxes.
In Rails you can have an auto_complete box act like a select like this…
-
# model
-
-
# id :integer
-
# name :string
-
# category_id :integer
-
# full_name :string
-
-
class Model < ActiveRecord::Base
-
belongs_to :category
-
validates_uniqueness_of :full_name
-
-
def before_save
-
self.full_name = "#{self.category.name} – #{self.name}"
-
end
-
end
-
# Controller
-
class ModelController < ActionController::Base
-
-
auto_complete_for :model, :full_name
-
-
def action
-
if request.post?
-
@model = Model.find_by_full_name(params[:model][:full_name])
-
# do some error handling here if item does not exist… or you could create a new one
-
else
-
@model = Model.new
-
end
-
-
end
-
# view for ‘action’
-
<%= start_form_tag :action=>’action’ %>
-
<%= text_field_with_auto_complete ‘model’, ‘full_name’ %>
-
<%= submit_tag ‘Submit’ %>
-
<%= end_form_tag %>
Now when the user starts typing in the auto_complete field, they will get records that match against both the name and the category of the record. The enterprising reader could also replace certain characters (like space) with the SQL wildcard ‘%’ to get more flexible search results.
A user might be able to just type: ‘f mus’ to find ‘Ford – Mustang’.
(It’s not well known, but ‘f%mus’ will match ‘Ford – Mustang’ using the standard auto_complete box that ships with rails).
This type of entry is really fast and quite intuitive once users get the hang of it.
I find that this system works best when
1. There are a ton of choices
2. The user already has a pretty good idea which choice they want to select, but don’t really want to spend 15 minutes scrolling to find it.
It’s not particularly good if the user has no idea what to enter. In those cases, the standard cascading select may help walk them through the selection process.
Notes: You can avoid using the ‘full_name’ column if you properly design your own custom auto_complete_for function
Filed by Kevin Olbrich at 3:41 pm under Ruby on Rails, User Interface, auto_complete, select controls
No Comments
38 Comments