问题
On new order form, a user needs to select a customer from a drop down list which contains dozens of customers and is hard to find and select by scrolling up and down. That's why we want to build a searchable drop down list
. When a user enter a few letters of customer's name, the drop down list filters out all others and is left with only the names of the customers containing the letters just keyed in. We are using simple_form
and rails 3.2.8
. One way we can do is to add a text box
for entering search string and call ajax
to load the drop down list accordingly. This requires additional text field which is redundant. Is there any solution which a user can directly type in drop down box and the list filters out accordingly? Thanks.
回答1:
Take a look at the JQuery UI autocomplete combobox. It provides both drop down functionality as well as search (autocomplete) functionality. It should work just fine with simple_form.
http://jqueryui.com/autocomplete/#combobox
The best example i've found of implementation is: http://babinho.net/2011/05/autocomplete-fields-in-rails-3-1-with-jquery-ui/
回答2:
I have quite good success with the Soulmate gem found https://github.com/seatgeek/soulmate. There's a well-written article which describes the gem too: http://patshaughnessy.net/2011/11/23/finding-your-soulmate-autocomplete-with-redis-in-rails-3-1
回答3:
If you don't already use (or don't want to use) jQuery UI in your project, you can use select2: http://ivaynberg.github.com/select2/
来源:https://stackoverflow.com/questions/12926330/searchable-drop-down-list-for-rails-3-2-8