This time I will share jQuery Plugin and tutorial about Versatile jQuery/Zepto Selection Library – Selectivity, hope it will help you in programming stack.
Selectivity (select3) is a jQuery/Zepto/React/Vanilla JavaScript alternative to Select2 that helps you create versatile & highly configurable select/input elements with the following features:
- Searchable dropdown select list.
- Multiple select.
- AJAX enabled.
- Filterable items.
- Tags/tokens input.
- Custom data type.
- Custom templates.
Installation:
5 |
$ npm install selectivity |
How to use it:
1. Include jQuery JavaScript library and the jQuery Selectivity plugin’s CSS & JavaScript in the document.
2 |
< link href = "/path/to/selectivity.min.css" rel = "stylesheet" > |
3 |
< script src = "/path/to/selectivity.min.js" ></ script > |
6 |
< link href = "/path/to/selectivity-jquery.min.css" rel = "stylesheet" > |
7 |
< script src = "/path/to/jquery.min.js" ></ script > |
8 |
< script src = "/path/to/selectivity-jquery.min.js" ></ script > |
2. Include the Font Awesome Iconic Font for essential icons.
1 |
< link href = "font-awesome.min.css" rel = "stylesheet" > |
3. Create an empty container to place the Selectivity component.
1 |
<span id= "demo" class= "example" ></span> |
4. Initialize the Selectivity component and define the data as follows:
1 |
$( '#demo' ).selectivity({ |
2 |
items: [ 'Item 1' , 'Item 2' , 'Item 3' ] |
5. You can also directly initialize the plugin on a select
element.
1 |
$( 'select' ).selectivity({ |
6. Option group (nested list) is supported as well.
02 |
$( '#demo' ).selectivity({ |
05 |
children: [ { id: 1, text: 'Item 1.1' } ] |
08 |
children: [ { id: 2, text: 'Item 2.1' } ] |
11 |
children: [ { id: 3, text: 'Item 3.1' } ] |
16 |
$( '#demo' ).selectivity({ |
21 |
items: [{ id: 2, text: 'Submenu 1' }], |
28 |
items: [{ id: 4, text: 'Submenu 2' }], |
7. Or load the data from an external data source:
01 |
$( '#demo' ).selectivity({ |
04 |
minimumInputLength: 3, |
06 |
params: function (term, offset) { |
08 |
return { q: term, page: 1 + Math.floor(offset / 30) }; |
10 |
fetch: function (url, init, queryOptions) { |
11 |
return $.ajax(url).then( function (data) { |
17 |
description: item.description |
20 |
more: (data.total_count > queryOptions.offset + data.items.length) |
25 |
placeholder: 'Search for a repository' , |
27 |
resultItem: function (item) { |
29 |
'<div class="selectivity-result-item" data-item-id="' + item.id + '">' + |
30 |
'<b>' + escape(item.text) + '</b><br>' + |
31 |
escape(item.description) + |
8. All configuration options.
ajax
: Load items through AJAX requests.
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
source : jquery.net