jQuery Dropdown Check List

The Dropdown Check List jQuery widget transforms a regular select html element into a dropdown checkbox list.

Dropdown Check List

Copyright © Adrian Tosca <adrian.tosca@gmail.com>

Licensed like jQuery, see http://docs.jquery.com/Licensing.

Download

Download current version can be downloaded from google code Downloads. This page including all required files to run the examples can be downloaded as zip package from the same location.

Dropdown check list requires jquery.js and ui.core.js and has been tested with jquery version 1.2.6. The widget has been tested with IE 7, Firefox 3, Opera 9.6, Safari 3.1 (for windows) and Chrome 0.3 browsers.

Example

The existing select element with id 's1' is replaced by a dropdown checkbox list widget. The widget will set hide the existing select element by modifying its display attribute to none. The new widget will keep the replaced select options synchronized with the checkbox list so the postback is not affected in any way. The text of the control is composed on the concatenated text of selected options in the list. Because the width of the control will not allways accomodate all selected options, the control will show the full text on hover by setting its title attribute.

$("#s1").dropdownchecklist();

Some option items from original select initially selected

The existing select element with id 's2' already has some options selected. In this case the widget will take over the selection automatically on initialization.

$("#s2").dropdownchecklist();

Long text, shorter control

An option allow to set a fixed width on the control even if the dropdown list is wider. The dropdown will keep its size so the items in the list are correctly visible.

$("#s3").dropdownchecklist({ width: 100 });

Long list of options, with scroll

Another option allow to set a fixed height of the dropdown list. This is usefull when there is a big number of items. The dropdown list is scrollable to allow selection of all the items.

$("#s4").dropdownchecklist({ maxDropHeight: 100 });

Option to let the first item in list check all items

The firstItemChecksAll option allow to set attach a special behavior to the first item in the list. If the item is checked, all items in the list are checked.

$("#s5").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 100 });

Select with groups

The existing select element with id 's6' has groups (optgroup elements). The options are indented in groups as with original select element.

$("#s6").dropdownchecklist();

Exclusive Options

The existing select element with id 's7' has some "exclusive" options. An option is made to be exclusive by adding the exclusive="true" attribute. When an exclusive option is checked, all other options are unchecked. Likewise, when any option is checked, any other options that are exclusive will be unchecked. This means that an option marked as exclusive cannot be checked with any other options.

$("#s7").dropdownchecklist();

Default Empty Text

The existing select element with id 's8' is set to show custom default text in the select box when nothing is checked.

$("#s8").dropdownchecklist({defaultText:'Choose...'});

Custom Select Text

The existing select element with id 's9' is set to show the text of the select box based on a custom function that uses the set of selections to determine what it wants to show.

            $("#s9").dropdownchecklist({
              customTextFn:function(opts) {
                var count = 0;
                var first = null;
                for (var i=0, len=opts.length; i<len; ++i) {
                  if (opts[i].selected) {
                    if (!first) {
                      first = opts[i].text;
                    }
                    ++count;
                  }
                }
                switch (count) {
                case 0:
                  return "Nobody";
                case 1:
                  return first;
                case opts.length:
                  return "Everybody";
                default:
                  return count + " People"
                }
              }
            });
        

Label-less Option Groups

The existing select element with id 's10' uses <optgroup> elements to group the options, but without specifying labels for them. In this case, the options are not indented, since the optgroup labels are not shown, and instead, the .ui-dropdownchecklist-group.nolabel class is applied to the groups, and CSS that is styled to show a horizontal separator between the groups.

$("#s10").dropdownchecklist();

Style

For more details see the example style sheet.