|  | 
| 1 |  | -jQuery Tokeninput: A Tokenizing Autocomplete Text Entry | 
| 2 |  | -======================================================= | 
| 3 | 1 | 
 | 
| 4 |  | -Overview | 
| 5 |  | --------- | 
| 6 |  | -Tokeninput is a jQuery plugin which allows your users to select multiple items from a predefined list, using autocompletion as they type to find each item. You may have seen a similar type of text entry when filling in the recipients field sending messages on facebook. | 
|  | 2 | +# jQuery Tokeninput: A Tokenizing Autocomplete Text Entry | 
| 7 | 3 | 
 | 
| 8 |  | -Documentation, Features and Demos | 
| 9 |  | ---------------------------------- | 
| 10 |  | -Full details and documentation can be found on the project page here: | 
|  | 4 | +## Overview | 
| 11 | 5 | 
 | 
| 12 |  | -<http://loopj.com/jquery-tokeninput/> | 
|  | 6 | +Tokeninput is a jQuery plugin which allows your users to select multiple items | 
|  | 7 | +from a predefined list, using autocompletion as they type to find each item. | 
|  | 8 | +You may have seen a similar type of text entry when filling in the recipients | 
|  | 9 | +field sending messages on facebook. | 
|  | 10 | + | 
|  | 11 | +## Features | 
|  | 12 | + | 
|  | 13 | +- Intuitive UI for selecting multiple items from a large list | 
|  | 14 | +- Easy to skin/style purely in css, no images required | 
|  | 15 | +- Supports any backend which can generate JSON, including PHP, Rails, Django, ASP.net | 
|  | 16 | +- Smooth animations when results load | 
|  | 17 | +- Select, delete and navigate items using the mouse or keyboard | 
|  | 18 | +- Client-side result caching to reduce server load | 
|  | 19 | +- Crossdomain support via JSONP | 
|  | 20 | +- Callbacks when items are added or removed from the list | 
|  | 21 | +- Preprocess results from the server with the onResult callback | 
|  | 22 | +- Programatically add, remove, clear and get tokens | 
|  | 23 | +- Customize the output format of the results and tokens | 
|  | 24 | + | 
|  | 25 | +## Screenshots | 
|  | 26 | + | 
|  | 27 | + | 
|  | 28 | + | 
|  | 29 | +Vertical list style item selection | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | + | 
|  | 33 | +Facebook style item selection | 
|  | 34 | + | 
|  | 35 | +## Installation & Setup | 
|  | 36 | + | 
|  | 37 | +### Create a server-side script to handle search requests | 
|  | 38 | + | 
|  | 39 | +Create a server-side script (PHP, Rails, ASP.net, etc) to generate your | 
|  | 40 | +search results. The script can fetch data from wherever you like, for | 
|  | 41 | +example a database or a hardcoded list. Your script must accept a GET parameter | 
|  | 42 | +named `q` which will contain the term to search for. E.g. | 
|  | 43 | +<http://www.example.com/myscript?q=query> | 
|  | 44 | + | 
|  | 45 | +Your script should output JSON search results in the following format: | 
|  | 46 | + | 
|  | 47 | +```javascript | 
|  | 48 | +[ | 
|  | 49 | +    {"id":"856","name":"House"}, | 
|  | 50 | +    {"id":"1035","name":"Desperate Housewives"}, | 
|  | 51 | +    ... | 
|  | 52 | +] | 
|  | 53 | +``` | 
|  | 54 | + | 
|  | 55 | +You may optionally specify an attribute of "readonly" and set it to true if you | 
|  | 56 | +would like some of the tokens to be non-removable: | 
|  | 57 | + | 
|  | 58 | +```javascript | 
|  | 59 | +[ | 
|  | 60 | +    {"id":"856","name":"House","readonly":true}, | 
|  | 61 | +    {"id":"1035","name":"Desperate Housewives"}, | 
|  | 62 | +    ... | 
|  | 63 | +] | 
|  | 64 | +``` | 
|  | 65 | + | 
|  | 66 | +Note that you may omit "readonly" on entities where it is not necessary. This | 
|  | 67 | +attribute is acceptable wherever JSON entities are passed, e.g. .tokenInput("add") | 
|  | 68 | +(see Methods section below). | 
|  | 69 | + | 
|  | 70 | +### Include and initialize the plugin | 
|  | 71 | + | 
|  | 72 | +Include jQuery and Tokeninput Javascript and stylesheet files on your page, and | 
|  | 73 | +attach to your text input: | 
|  | 74 | +Tokeninput stylesheet: | 
|  | 75 | + | 
|  | 76 | +```html | 
|  | 77 | +<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> | 
|  | 78 | +<script type="text/javascript" src="yourfiles/jquery.tokeninput.js"></script> | 
|  | 79 | +<link rel="stylesheet" type="text/css" href="yourfiles/token-input.css" /> | 
|  | 80 | + | 
|  | 81 | +<script type="text/javascript"> | 
|  | 82 | +$(document).ready(function () { | 
|  | 83 | +    $("#my-text-input").tokenInput("/url/to/your/script/"); | 
|  | 84 | +}); | 
|  | 85 | +</script> | 
|  | 86 | +``` | 
|  | 87 | + | 
|  | 88 | +## Configuration | 
|  | 89 | + | 
|  | 90 | +The tokeninput takes an optional second parameter on intitialization which | 
|  | 91 | +allows you to customize the appearance and behaviour of the script, as well as | 
|  | 92 | +add your own callbacks to intercept certain events. The following options are | 
|  | 93 | +available: | 
|  | 94 | + | 
|  | 95 | +### Search Settings | 
|  | 96 | + | 
|  | 97 | +method | 
|  | 98 | +:   The HTTP method (eg. GET, POST) to use for the server request. *default: | 
|  | 99 | +    "GET"*. | 
|  | 100 | + | 
|  | 101 | +queryParam | 
|  | 102 | +:   The name of the query param which you expect to contain the search term | 
|  | 103 | +    on the server-side. *default: "q"*. | 
|  | 104 | + | 
|  | 105 | +searchDelay | 
|  | 106 | +:   The delay, in milliseconds, between the user finishing typing and the | 
|  | 107 | +    search being performed. *default: 300* | 
|  | 108 | + | 
|  | 109 | +minChars | 
|  | 110 | +:   The minimum number of characters the user must enter before a search is | 
|  | 111 | +    performed. *default: 1* | 
|  | 112 | + | 
|  | 113 | +propertyToSearch | 
|  | 114 | +:   The javascript/json object attribute to search. | 
|  | 115 | +    *default: "name"* | 
|  | 116 | + | 
|  | 117 | +jsonContainer | 
|  | 118 | +:   The name of the json object in the response which contains the search | 
|  | 119 | +    results. This is typically used when your endpoint returns other data in | 
|  | 120 | + addition to your search results. | 
|  | 121 | + Use `null` to use the top level response object. *default: null*. | 
|  | 122 | + | 
|  | 123 | +crossDomain | 
|  | 124 | +:   Force JSONP cross-domain communication to the server instead of a normal | 
|  | 125 | +    ajax request. Note: JSONP is automatically enabled if we detect the search | 
|  | 126 | + request is a cross-domain request. *default: false*. | 
|  | 127 | + | 
|  | 128 | +### Pre-population Settings | 
|  | 129 | + | 
|  | 130 | +prePopulate | 
|  | 131 | +:   Prepopulate the tokeninput with existing data. Set to an array of JSON | 
|  | 132 | +    objects, eg: `[{id: 3, name: "test"}, {id: 5, name: "awesome"}]` | 
|  | 133 | +    to pre-fill the input. *default: null* | 
|  | 134 | + | 
|  | 135 | +### Display Settings | 
|  | 136 | + | 
|  | 137 | +hintText | 
|  | 138 | +:   The text to show in the dropdown label which appears when you first click | 
|  | 139 | +    in the search field. *default: "Type in a search term"* | 
|  | 140 | + | 
|  | 141 | +noResultsText | 
|  | 142 | +:   The text to show in the dropdown label when no results are found which | 
|  | 143 | +    match the current query. *default: "No results"* | 
|  | 144 | + | 
|  | 145 | +searchingText | 
|  | 146 | +:   The text to show in the dropdown label when a search is currently in | 
|  | 147 | +    progress. *default: "Searching..."* | 
|  | 148 | + | 
|  | 149 | +deleteText | 
|  | 150 | +:   The text to show on each token which deletes the token when clicked. If | 
|  | 151 | + you wish to hide the delete link, provide an empty string here. | 
|  | 152 | + Alternatively you can provide a html string here if you would like to | 
|  | 153 | + show an image for deleting tokens. | 
|  | 154 | +    *default: ×* | 
|  | 155 | + | 
|  | 156 | +animateDropdown | 
|  | 157 | +:   Set this to `false` to disable animation of the dropdown *default: true* | 
|  | 158 | + | 
|  | 159 | +theme | 
|  | 160 | +:   Set this to a string, eg "facebook" when including theme css files to set | 
|  | 161 | +    the css class suffix | 
|  | 162 | + | 
|  | 163 | +resultsLimit | 
|  | 164 | +:   The maximum number of results shown in the drop down. Use `null` to show | 
|  | 165 | +    all the matching results. *default: null* | 
|  | 166 | + | 
|  | 167 | +resultsFormatter | 
|  | 168 | +:   A function that returns an interpolated HTML string for each result. Use | 
|  | 169 | +    this function with a templating system of your choice, such as jresig | 
|  | 170 | +    microtemplates or mustache.js. Use this when you want to include images or | 
|  | 171 | +    multiline formatted results | 
|  | 172 | +    *default: `function(item){ return "<li>" + item.propertyToSearch + "</li>" }`* | 
|  | 173 | + | 
|  | 174 | +tokenFormatter | 
|  | 175 | +:   A function that returns an interpolated HTML string for each token. Use | 
|  | 176 | +    this function with a templating system of your choice, such as jresig | 
|  | 177 | +    microtemplates or mustache.js. Use this when you want to include images or | 
|  | 178 | +    multiline formatted tokens. Quora's people invite token field that returns | 
|  | 179 | +    avatar tokens is a good example of what can be done this option. | 
|  | 180 | +    *default: `function(item){ return "<li><p>" + item.propertyToSearch + "</p></li>" }`* | 
|  | 181 | + | 
|  | 182 | +### Tokenization Settings | 
|  | 183 | + | 
|  | 184 | +tokenLimit | 
|  | 185 | +:   The maximum number of results allowed to be selected by the user. Use | 
|  | 186 | +    `null` to allow unlimited selections. *default: null* | 
|  | 187 | + | 
|  | 188 | +tokenDelimiter | 
|  | 189 | +:  The separator to use when sending the results back to the server. | 
|  | 190 | + *default: ","*. | 
|  | 191 | + | 
|  | 192 | +preventDuplicates | 
|  | 193 | +:   Prevent user from selecting duplicate values by setting this to `true`. | 
|  | 194 | +    *default: false* | 
|  | 195 | + | 
|  | 196 | +tokenValue | 
|  | 197 | +:   The value of the token input when the input is submitted. Set it to `id` | 
|  | 198 | +    in order to get a concatenation of token IDs, or to `name` in order to | 
|  | 199 | +    get a concatenation of names. *default: id* | 
|  | 200 | + | 
|  | 201 | +### Callbacks | 
|  | 202 | + | 
|  | 203 | +onResult | 
|  | 204 | +:   A function to call whenever we receive results back from the server. You | 
|  | 205 | +    can use this function to pre-process results from the server before they | 
|  | 206 | +    are displayed to the user. *default: null* | 
|  | 207 | + | 
|  | 208 | +onAdd | 
|  | 209 | +:   A function to call whenever the user adds another token to their | 
|  | 210 | +    selections. *defaut: null* | 
|  | 211 | + | 
|  | 212 | +onDelete | 
|  | 213 | +:   A function to call whenever the user removes a token from their selections. | 
|  | 214 | +    *default: null* | 
|  | 215 | + | 
|  | 216 | +onReady | 
|  | 217 | +:   A function to call after initialization is done and the tokeninput is ready | 
|  | 218 | +    to use. *default: null* | 
|  | 219 | + | 
|  | 220 | +## Methods | 
|  | 221 | + | 
|  | 222 | +`selector.tokenInput("add", {id: x, name: y});` | 
|  | 223 | +:   Add a new token to the tokeninput with id `x` and name `y`. | 
|  | 224 | + | 
|  | 225 | +`selector.tokenInput("remove", {id: x});` | 
|  | 226 | +:   Remove the tokens with id `x` from the tokeninput. | 
|  | 227 | + | 
|  | 228 | +`selector.tokenInput("remove", {name: y});` | 
|  | 229 | +:   Remove the tokens with name `y` from the tokeninput. | 
|  | 230 | + | 
|  | 231 | +`selector.tokenInput("clear");` | 
|  | 232 | +:   Clear all tokens from the tokeninput. | 
|  | 233 | + | 
|  | 234 | +`selector.tokenInput("get");` | 
|  | 235 | +:   Gets the array of selected tokens from the tokeninput (each item being an object of the kind `{id: x, name: y}`). | 
|  | 236 | + | 
|  | 237 | +## Reporting Bugs or Feature Requests | 
|  | 238 | + | 
|  | 239 | +Please report any bugs or feature requests on the github issues page for this | 
|  | 240 | +project here: | 
|  | 241 | + | 
|  | 242 | +<https://github.com/loopj/jquery-tokeninput/issues> | 
|  | 243 | + | 
|  | 244 | +## License | 
|  | 245 | + | 
|  | 246 | +Tokeninput is released under a dual license. You can choose either the GPL or | 
|  | 247 | +MIT license depending on the project you are using it in and how you wish to | 
|  | 248 | +use it. | 
0 commit comments