andreww1011 / filter-multi-select

Multiple select dropdown with filter jQuery plugin.
Other
28 stars 15 forks source link
filter filter-jquery-plugin filter-multi-select javascript jquery select select-all select-multiple typescript

filter-multi-select

Multiple select dropdown with filter jQuery plugin.

JSFiddle

Improve the useability of HTML <select> elements:

Uses Bootstrap classes for styling. Easily modify CSS to match your theme.

Usage

  1. Load jQuery, Bootstrap, and the plugin bundle in your HTML code.
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
    ...
    <link rel="stylesheet" href="https://github.com/andreww1011/filter-multi-select/blob/main/filter_multi_select.css"/>
    <script src="https://github.com/andreww1011/filter-multi-select/raw/main/filter-multi-select-bundle.min.js"></script>
  2. Define a <select> element with name and multiple attributes in your HTML code. Supported optional attributes: disabled - disables the dropdown.
  3. Define <option>'s with unique value attributes. Supported optional attributes: label - alternate dropdown display text; selected - pre-select this option; disabled - disable this option.
    <form>
      ...
      <select multiple id="pets" name="pets">
        <option value="0">Doge</option>
        <option value="1" selected>Keyboard Cat</option>
        <option value="2" label="Badger" disabled>Badger Badger Badger</option>
        ...
  4. Use jQuery to target the <select> and apply the plugin. The HTML is replaced by the plugin in the DOM.
    <script>
      $(function () {
        $('#pets').filterMultiSelect();
      });
    </script>
  5. Or append the class filter-multi-select to the select element and have it be targeted automatically.
    <select class="filter-multi-select" multiple id="pets" name="pets">
  6. Listen to the events optionselected and optiondeselected to signal changes.
    <script>
      $(function () {
        $('#pets').on('optionselected', function(e) {
          ...
        });
      });
    </script>

# Options

The following indexed parameters can be passed to filterMultiSelect() at construction.

# API

The following methods are exposed on the plugin:

The following global fields are exposed on the jQuery extension point:

The following data is available on the optionselected and optiondeselected event object e: