Chosen is a JavaScript plugin that makes long, unwieldy select boxes much more user-friendly. It is currently available in both jQuery and Prototype flavors.
http://harvesthq.github.com/chosen/Default Text Support
Chosen automatically sets the default field text (“Choose a
country…”) by reading the select element’s data-placeholder value. If no
data-placeholder value is present, it will default to “Select Some
Option” or “Select Some Options” depending on whether the select is
single or multiple. You can change these elements in the plugin js file
as you see fit.
<select data-placeholder="Choose a country..." style="width:350px;" multiple">
Note:
on single selects, the first element is assumed to be selected by the
browser. To take advantage of the default text support, you will need to
include a blank option as the first element of your select list.No Results Text Support
Setting the “No results” search text is as easy as passing an option when you create Chosen:
$(".chzn-select").chosen({no_results_text: "No results matched"}); // jQuery version
New Chosen($("chzn_select_field"),{no_results_text: "No results matched"}); // Prototype version
Allow Deselect on Single Selects
Change / Update Events
-
Form Field Change
When working with form fields, you often want to perform some behavior after a value has been selected or deselected. Whenever a user selects a field in Chosen, it triggers a “change” event* on the original form field. That let’s you do something like this:
$("#form_field").chosen().change( … );
Note: Prototype doesn’t offer support for triggering standard browser events. Event.simulate is required to trigger the change event when using the Prototype version. -
Updating Chosen Dynamically
If you need to update the options in your select field and want Chosen to pick up the changes, you’ll need to trigger the “liszt:updated” event on the field. Chosen will re-build itself based on the updated content.
- jQuery Version:
$("#form_field").trigger("liszt:updated");
- Prototype Version:
Event.fire($("form_field"), "liszt:updated");
- jQuery Version:
Why use Chosen?
-
User Friendly
Instead of forcing your users to scroll through a giant list of items, they can just start typing the name of the item they were looking for. Non-matching entries are removed from view and choices can be selected using enter or mouse click. -
Progressive Enhancement
Because chosen replaces normal html select fields, you don’t need to do anything special to make it work for browsers without JavaScript. You don’t need to do anything special on the back end to handle the data either — the form field still gets submitted as normal. -
Painless Setup
Add Chosen’s files to your app and trigger the plugin (see Setup below). Chosen automatically respects optgroups, selected state, the multiple attribute and browser tab order. You don’t need to do anything else except customize the style as you see fit.
Setup
Using Chosen is easy as can be.- Download the plugin and copy the related files to your app.
- Activate the plugin:
- jQuery Version:
Call the chosen plugin:$(".chzn-select").chosen();
- Prototype Version:
Create a new instance of Chosen:new Chosen(some_form_element);
- jQuery Version:
- Disco.
No comments:
Post a Comment