Versatile jQuery/Zepto Selection Library - Selectivity

Versatile jQuery/Zepto Selection Library - Selectivity
File Size: 570 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

Selectivity (select3) is a jQuery/Zepto alternative to Select2 that helps you create versatile & highly configurable select/input elements with the following features:

  • Seachable dropdown select list.
  • Multiple select.
  • Tags/tokens input.
  • Custom data type.
  • Custom templates.

Installation:

# Yarn
yarn add selectivity

# NPM
$ npm install selectivity

How to use it:

1. Include jQuery JavaScript library and the jQuery Selectivity plugin's CSS & JavaScript in the document.

<link href="dist/selectivity-full.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="dist/selectivity-full.js"></script>

2. Include Font Awesome 4 for essential icons.

<linkhref="//netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">

3. Create an inline element that will be turned into a beautiful select/input element.

<span id="demo" class="select3-input"></span>

4. Call the plugin on the element.

$('#demo').selectivity({
// OPTIONS
);

5. Options and defaults.

  • allowClear: Set to true to allow the selection to be cleared. This option only applies to single-value inputs.
  • backspaceHighlightsBeforeDelete: If set to true, when the user enters a backspace while there is no text in the search field but there are selected items, the last selected item will be highlighted and when a second backspace is entered the item is deleted. If false, the item gets deleted on the first backspace. The default value is true on devices that have touch input and false on devices that don't.
  • closeOnSelect: Set to false to keep the dropdown open after the user has selected an item. This is useful if you want to allow the user to quickly select multiple items. The default value is true.
  • createTokenItem: Function to create a new item from a user's search term. This is used to turn the term into an item when dropdowns are disabled and the user presses Enter. It is also used by the default tokenizer to create items for individual tokens. The function receives a token parameter which is the search term (or part of a search term) to create an item for and must return an item object with id and text properties or null if no token can be created from the term. The default is a function that returns an item where the id and text both match the term for any non-empty string and which returns null otherwise. This option only applies to multiple-value inputs.
  • data: Initial selection data to set. This should be an object with id and text properties in the case of input type 'Single', or an array of such objects otherwise. This option is mutually exclusive with value.
  • dropdown: Custom dropdown implementation to use for this instance
  • dropdownCssClass: Optional CSS class to add to the top-level dropdown element.
  • initSelection: Function to map values by ID to selection data. This function receives two arguments, value and callback. The value is the current value of the selection, which is an ID or an array of IDs depending on the input type. The callback should be invoked with an object or array of objects, respectively, containing id and text properties.
  • inputType: The input type to use. Default input types include 'Multiple', 'Single' and 'Email', but you can add custom input types to the Select3.InputTypes map or just specify one here as a function. The default value is 'Single', unless multiple is true in which case it is 'Multiple'.
  • items: Array of items from which to select. Should be an array of objects with id and text properties. As convenience, you may also pass an array of strings, in which case the same string is used for both the ID and the text. If items are given, all items are expected to be available locally and all selection operations operate on this local array only. If null, items are not available locally, and the query option should be provided to fetch remote data.
  • matcher: Function to determine whether text matches a given search term. Note this function is only used if you have specified an array of items. Receives two arguments: item and term. The item is the item that should match the search term. The term is the search term, which for performance reasons has always been already processed using Select3.transformText(). The method should return the item if it matches, and null otherwise. If the item has a children array, the matcher is expected to filter those itself (be sure to only return the filtered array of children in the returned item and not to modify the children of the item argument).
  • placeholder: Placeholder text to display when the element has no focus and selected items.
  • positionDropdown: Function to position the dropdown. Receives $dropdownEl (the element to be positioned) and $selectEl (the element of the Select3 instance) as arguments. The default implementation positions the dropdown element under the Select3's element and gives it the same width as well.
  • query: Function to use for querying items. Receives a single object as argument with the callback, offset and term properties. The callback should be invoked when the results are available. It should be passed a single object with results and more properties. The first is an array with result items and the latter is an optional boolean that may be set to true indicate more results are available through pagination. Offset is a property is only used for pagination and indicates how many results should be skipped when returning more results. The term is the search term the user is searching for. Unlike with the matcher function, the term has not been processed using Select3.transformText(). This option is ignored if the items option is used.
  • searchInputPlaceholder: Placeholder text to display in the search input in the dropdown.
  • showDropdown: Set to false if you don't want to use any dropdown (you can still open still open it programmatically using open()).
  • showSearchInputInDropdown: Set to false to remove the search input used in dropdowns. This option only applies to single-value inputs, as multiple-value inputs don't have the search input in the dropdown to begin with. The default is true.
  • suppressMouseWheelSelector: The Select3 Dropdown by default suppresses mousewheel events so that any scrolling in the dropdown doesn't affect the scroll position of the page. Through this option you can select which selector should be monited for scroll events to suppress. Set it to null to disable suppressing of mousewheel events altogether. The default value is ".select3-results-container".
  • templates: Object with instance-specific templates to override the global templates assigned to Select3.Templates.
  • tokenizer: Function for tokenizing search terms. Will receive the input (the input string to tokenize), selection (the current selection data), createToken (callback to create a token from the search terms, should be passed an item object with id and text properties) and options (the options set on the Select3 instance) arguments. Any string returned by the tokenizer function is treated as the remainder of untokenized input. This option only applies to multiple-value inputs.
  • tokenSeparators: Array of string separators which are used to separate the search term into tokens. If specified and the tokenizer property is not set, the tokenizer property will be set to a function which splits the search term into tokens separated by any of the given separators. The tokens will be converted into selectable items using the createTokenItem function. The default tokenizer also filters out already selected items. This option only applies to multiple-value inputs.
  • value: Initial value to set. This should be an ID (string or number) in the case of input type 'Single', or array of IDs otherwise. This property is mutually exclusive with data.

6. Events available.

  • "change": Emitted when the selection has been changed. Additional properties on the event object: added, removed and value.
  • "select3-closed": Emitted when the dropdown is closed.
  • "select3-highlight": Emitted when an item in the dropdown is highlighted. Additional properties on the event object: item and value.
  • "select3-open": mitted when the dropdown is opened.
  • "select3-opening": Emitted when the dropdown is about to be opened. You can call preventDefault() on the event object to cancel the opening of the dropdown.
  • "select3-selected": Emitted when an item has been selected. Additional properties on the event object: id and item.
  • "select3-selecting": Emitted when an item is about to be selected. You can call preventDefault() on on the event object to prevent the item from being selected. Additional properties on the event object: id and item.

Change logs:

v3.0.5 (2017-08-21)

  • Fix issue where dropdown is hard to close on Firefox
  • Filter selected children from results
  • Move submenu up if otherwise it would fall below the fold of the page

v3.0.4 (2017-04-27)

  • Improved compatibility with React 15.5
  • Improved HTML5 validation support
  • Fix: Existing selection highlighted by default on multi-value search results
  • Fix: Don't close dropdown when clicking scrollbar

v3.0.3 (2017-04-20)

  • Fix: Existing selection highlighted by default on multi-value search results.
  • Fix: Don't close dropdown when clicking scrollbar.
  • Fix issue when a single-value Selectivity input is reset to null throught the React API.
  • Fix: React API: Value should be re-set when the items change.
  • Fix: Don't crash when unsubscribing from non-subscribed event listener.
  • Don't rely on react-dom-server in React templates plugin to avoid issues with React 15.4.
  • Fix: Expose Selectivity object as $.Selectivity in jQuery builds.
  • Yarn compatibility: Get rid of peerDependencies.

v3.0.0 (2016-08-04)

  • Made jQuery dependency fully optional.
  • Added optional React dependency.
  • Added option: shouldOpenSubmenu() - Callback that determines whether a submenu should be opened.
  • Added option: selectable - Allows to make items unselectable without having to disable them. This is mainly useful for items that trigger submenus.
  • Fixed: Added NPM package.
  • Removed Bower and Component support.
  • Removed dist directory from the repository.
  • Moved option validation into its own plugin.
  • Fixed: Remove the dropdown after timeout to fix "hover" behavior.
  • Fixed: Update original <select> element on "change" instead of "selectivity-selected".

v2.1.0 (2016-01-06)

  • Implemented disabled property on items. When an item is disabled it cannot be selected and by default is rendered with grey text.
  • Fix problem with closeOnSelect behavior.
  • Added CSS classes for hover and open states.
  • Fix: Respect removeOnly option when set after initialization.
  • Fix: Pass queryOptions to url function in AJAX module.
  • Fix: Make sure Enter key doesn't submit forms.
  • Fix: Make the rerenderSelection() method public and document that triggerChange: false doesn't automatically update the UI.
  • Fixed issue where the cursor position was constantly reset when using a tokenizer.
  • Miscellaneous smaller fixes and styling tweaks.

v2.0.0 (2016-01-06)

  • Fix recursion problems and some issues with submenus.
  • Pass correct selectivity reference to query functions.
  • Implement disabled items.

2015-05-15

  • Fix loading more on scroll.
  • Make sure items at the bottom don't get cut off with jQuery.

2015-04-30

  • Move close icon to the left.
  • Fixed: Incorrect behavior of backspace when deleting last character from search input.
  • When query option is specified in options, don't use the value of options to initialize items array.

2015-04-06

  • Don't try to maintain focus when the user is tabbing to the next input.
  • Use a hidden input field to improve keyboard support (tab navigation and general keyboard support for single-value selectors without search input).

2015-03-17

  • rename to selectivity

2015-03-10

  • v1.0.0

2015-02-12

  • fixed bugs and update.

2015-02-01

  • Fix bug with initialization of traditional <select> boxes

2015-01-30

  • Fix placeholder support in traditional <select> boxes.

2015-01-29

  • Fix opening dropdown when showSearchInputInDropdown is false

This awesome jQuery plugin is developed by arendjr. For more Advanced Usages, please check the demo page or visit the official website.