This time I will share jQuery Plugin and tutorial about Dynamic Token/Tag Input With Autocomplete – jQuery Tokeninput, hope it will help you in programming stack.
The jQuery Tokeninput plugin transforms the regular text field into a dynamic, easy-to-style token/tag input with autocomplete/typeahead support. The users are allowed to select multiple tokens/tags from a predefined list that loads data from local or remote dataset via AJAX.
How to use it:
1. Load the main JavaScript and CSS files in the html document.
1 |
< script src = "jquery.min.js" ></ script > |
2 |
< script src = "jquery.tokeninput.js" ></ script > |
3 |
< link rel = "stylesheet" href = "token-input.css" > |
2. Load a theme CSS of your choice in the document. The plugin currently comes with 2 themes: Facebook and Mac:
1 |
< link rel = "stylesheet" href = "token-input-facebook.css" > |
2 |
< link rel = "stylesheet" href = "token-input-mac.css" > |
3. Create a normal text input field in the document.
1 |
< input type = "text" id = "demo-input" > |
4. Activate the token/tag input that loads autocomplete data from local JS objects.
01 |
$( "#demo-input" ).tokenInput([ |
02 |
{id: 7, name: "Ruby" }, |
03 |
{id: 11, name: "Python" }, |
04 |
{id: 13, name: "JavaScript" }, |
05 |
{id: 17, name: "ActionScript" }, |
06 |
{id: 19, name: "Scheme" }, |
07 |
{id: 23, name: "Lisp" }, |
09 |
{id: 31, name: "Fortran" }, |
10 |
{id: 37, name: "Visual Basic" }, |
12 |
{id: 43, name: "C++" }, |
13 |
{id: 47, name: "Java" } |
5. Activate the token/tag input that loads autocomplete data from external data source via AJAX requests.
1 |
$( "#demo-input" ).tokenInput( "json.php" , { |
6. All default plugin options and callback functions.
01 |
$( "#demo-input" ).tokenInput({ |
08 |
propertyToSearch: "name" , |
11 |
excludeCurrent: false , |
12 |
excludeCurrentParameter: "x" , |
16 |
processPrePopulate: false , |
19 |
hintText: "Type in a search term" , |
20 |
noResultsText: "No results" , |
21 |
searchingText: "Searching..." , |
23 |
animateDropdown: true , |
31 |
resultsFormatter: function (item) { |
32 |
var string = item[ this .propertyToSearch]; |
33 |
return "<li>" + ( this .enableHTML ? string : _escapeHTML(string)) + "</li>" ; |
36 |
tokenFormatter: function (item) { |
37 |
var string = item[ this .propertyToSearch]; |
38 |
return "<li><p>" + ( this .enableHTML ? string : _escapeHTML(string)) + "</p></li>" ; |
44 |
preventDuplicates: false , |
48 |
allowFreeTagging: false , |
50 |
autoSelectFirstResult: false , |
56 |
onFreeTaggingAdd: null , |
61 |
idPrefix: "token-input-" , |
7. API methods.
01 |
var myToken = $( "#demo-input" ).tokenInput(); |
04 |
myToken.tokenInput( "add" , {id: x, name: y}); |
07 |
selector.tokenInput( "remove" , {id: x}); |
08 |
selector.tokenInput( "remove" , {name: y}); |
11 |
selector.tokenInput( "clear" ); |
source : jqueryscript.net
|