Today I want to present a very convenient jQuery plugin I’ve used to create a combo box with checkboxes for a multi-selection field.
Suppose we have a form of insertion / modification of user data, and that the user can have multiple functions.
We start with rendering the form with a multi-selection field, where you can select more functions with the combination “<ctrl> + click “.
1 2 3 4 5 6 7 8 9 10 11 12 | <%= simple_form_for(@user) do |f| %> |
In the above code I used the simple_form plugin to simplify the creation of the form.
The controller will contains something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class UsersController < ApplicationController |
Now we need some of jQuery to make our form more pleasing.
We download the jQuery UI MultiSelect Widget plugin and then we include it in our pages, such as in the file application.html.erb as follows:
1 2 3 | [...] |
Finally, simply insert at the end of the page containing the user creation form, the following script:
1 2 3 4 5 | <script type="text/javascript"> |
We reload the page and we’ll obtain something like the screenshot below.
This example was tested on a Rails 3 application with JQuery 1.4.2.
Looking for rental properties try real estate alameda.