• This repository has been archived on 10/Sep/2023
  • Stars
    star
    2,364
  • Rank 19,455 (Top 0.4 %)
  • Language
    JavaScript
  • Created over 15 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jQuery plugin which allows your users to select multiple items from a predefined list

jQuery Tokeninput: A Tokenizing Autocomplete Text Entry

Overview

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.

Features

  • Intuitive UI for selecting multiple items from a large list
  • Easy to skin/style purely in css, no images required
  • Supports any backend which can generate JSON, including PHP, Rails, Django, ASP.net
  • Smooth animations when results load
  • Select, delete and navigate items using the mouse or keyboard
  • Client-side result caching to reduce server load
  • Crossdomain support via JSONP
  • Callbacks when items are added or removed from the list
  • Preprocess results from the server with the onResult callback
  • Programatically add, remove, clear and get tokens
  • Customize the output format of the results and tokens

Screenshots

List style

Vertical list style item selection

List style

Facebook style item selection

Installation & Setup

Create a server-side script to handle search requests

Create a server-side script (PHP, Rails, ASP.net, etc) to generate your search results. The script can fetch data from wherever you like, for example a database or a hardcoded list. Your script must accept a GET parameter named q which will contain the term to search for. E.g. http://www.example.com/myscript?q=query

Your script should output JSON search results in the following format:

[
    {"id":"856","name":"House"},
    {"id":"1035","name":"Desperate Housewives"},
    ...
]

You may optionally specify an attribute of "readonly" and set it to true if you would like some of the tokens to be non-removable:

[
    {"id":"856","name":"House","readonly":true},
    {"id":"1035","name":"Desperate Housewives"},
    ...
]

Note that you may omit "readonly" on entities where it is not necessary. This attribute is acceptable wherever JSON entities are passed, e.g. .tokenInput("add") (see Methods section below).

Include and initialize the plugin

Include jQuery and Tokeninput Javascript and stylesheet files on your page, and attach to your text input: Tokeninput stylesheet:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="yourfiles/jquery.tokeninput.js"></script>
<link rel="stylesheet" type="text/css" href="yourfiles/token-input.css" />

<script type="text/javascript">
$(document).ready(function () {
    $("#my-text-input").tokenInput("/url/to/your/script/");
});
</script>

Configuration

The tokeninput takes an optional second parameter on intitialization which allows you to customize the appearance and behaviour of the script, as well as add your own callbacks to intercept certain events. The following options are available:

Search Settings

Option Description Default
method The HTTP method (eg. GET, POST) to use for the server request. "GET"
queryParam The name of the query param which you expect to contain the search term on the server-side. "q"
searchDelay The delay, in milliseconds, between the user finishing typing and the search being performed. 300
minChars The minimum number of characters the user must enter before a search is performed. 1
propertyToSearch The javascript/json object attribute to search. "name"
jsonContainer The name of the json object in the response which contains the search results. This is typically used when your endpoint returns other data in addition to your search results. Use null to use the top level response object. null
crossDomain Force JSONP cross-domain communication to the server instead of a normal ajax request. Note: JSONP is automatically enabled if we detect the search request is a cross-domain request. false

Pre-population Settings

Option Description Default
prePopulate Prepopulate the tokeninput with existing data. Set to an array of JSON objects, eg: [{id: 3, name: "test"}, {id: 5, name: "awesome"}] to pre-fill the input. null

Display Settings

Option Description Default
hintText The text to show in the dropdown label which appears when you first click in the search field. "Type in a search term"
noResultsText The text to show in the dropdown label when no results are found which match the current query. "No results"
searchingText The text to show in the dropdown label when a search is currently in progress. "Searching..."
deleteText The text to show on each token which deletes the token when clicked. If you wish to hide the delete link, provide an empty string here. Alternatively you can provide a html string here if you would like to show an image for deleting tokens. "&times;"
animateDropdown Set this to false to disable animation of the dropdown true
theme Set this to a string, eg "facebook" when including theme css files to set the css class suffix
resultsLimit The maximum number of results shown in the drop down. Use null to show all the matching results. null
resultsFormatter A function that returns an interpolated HTML string for each result. Use this function with a templating system of your choice, such as jresig microtemplates or mustache.js. Use this when you want to include images or multiline formatted results function(item){ return "<li>" + item.propertyToSearch + "</li>" }
tokenFormatter A function that returns an interpolated HTML string for each token. Use this function with a templating system of your choice, such as jresig microtemplates or mustache.js. Use this when you want to include images or multiline formatted tokens. Quora's people invite token field that returns avatar tokens is a good example of what can be done this option. function(item){ return "<li><p>" + item.propertyToSearch + "</p></li>" }

Tokenization Settings

Option Description Default
tokenLimit The maximum number of results allowed to be selected by the user. Use null to allow unlimited selections. null
tokenDelimiter The separator to use when sending the results back to the server. ","
preventDuplicates Prevent the user from selecting a token that has already been selected? false
tokenValue The value of the token input when the input is submitted. Set it to id in order to get a concatenation of token IDs, or to name in order to get a concatenation of names. "id"

Callbacks

Option Description Default
onResult A function to call whenever we receive results back from the server. You can use this function to pre-process results from the server before they are displayed to the user. null
onAdd A function to call whenever the user adds another token to their selections. null
onDelete A function to call whenever the user removes a token from their selections. null
onReady A function to call after initialization is done and the tokeninput is ready to use. null

Methods

Method Description
selector.tokenInput("add", {id: x, name: y}); Add a new token to the tokeninput with id x and name y.
selector.tokenInput("remove", {id: x}); Remove the tokens with id x from the tokeninput.
selector.tokenInput("remove", {name: y}); Remove the tokens with name y from the tokeninput.
selector.tokenInput("clear"); Clear all tokens from the tokeninput.
selector.tokenInput("get"); Gets the array of selected tokens from the tokeninput (each item being an object of the kind {id: x, name: y}).

Reporting Bugs or Feature Requests

Please report any bugs or feature requests on the github issues page for this project here:

https://github.com/loopj/jquery-tokeninput/issues

License

Tokeninput is released under a dual license. You can choose either the GPL or MIT license depending on the project you are using it in and how you wish to use it.

More Repositories

1

android-smart-image-view

Android ImageView replacement which allows image loading from URLs or contact address book, with caching
Java
1,295
star
2

short-stack

World's Smallest Nintendo Wii, using a trimmed motherboard and custom stacked PCBs
C
707
star
3

capistrano-node-deploy

Capistrano recipe for deploying node.js apps
Ruby
197
star
4

jquery-simple-slider

Unobtrusive numerical slider plugin for jQuery
JavaScript
194
star
5

node-sidekiq

Enqueue jobs to sidekiq from your node apps. Closely mirrors the official ruby sidekiq interface and supports job scheduling.
CoffeeScript
104
star
6

airbrake-android

Airbrake Notifier for Android
Java
56
star
7

proguard-gradle-example

Example app showing how to use proguard with gradle
Java
51
star
8

initials-avatar

Generate an avatar image from a user's initials, ideal for use as a meaningful gravatar fallback.
Ruby
43
star
9

commonjs-ansi-color

ANSI color terminal output module for CommonJS (including Node.js)
JavaScript
40
star
10

i2c-hal

Easily use sensors like accelerometers, gyroscopes & barometers in your Arduino, ESP8266, mbed, Particle & Raspberry Pi projects
C++
27
star
11

aa-recruiter-honeypot

A Honeypot Repo for Recruiters
24
star
12

commonjs-date-formatting

PHP style strftime date formatting for CommonJS apps.
JavaScript
16
star
13

opengl-racing-game

Simple OpenGL racing game I made back in 2001, just rediscovered the dusty zip file
C
15
star
14

node-simple-redis-queue

JavaScript
15
star
15

open-rts

A full implementation of the Somfy RTS protocol designed for creating your own remotes, receivers, repeaters, or bridges on embedded hardware
C
15
star
16

wii-power-strip

Slimline PCB to supply power to trimmed Wii motherboards
10
star
17

connect-url-rewrite

Connect/Express middleware for rewriting URLs using regular expressions (like mod_rewrite).
JavaScript
9
star
18

gitscore

gitscore - github reputation app
CSS
7
star
19

periphlex

Flex PCB breakout for Bluetooth, USB, GameCube controller ports on trimmed Wii motherboards
7
star
20

home-assistant-vantage

Home Assistant integration for Vantage InFusion home automation controllers.
Python
6
star
21

console.frog

A bad pun for your JavaScript console
JavaScript
6
star
22

tcpclient

Arduino-Style POSIX TCP Client
C++
6
star
23

aiovantage

Python library for interacting with and controlling Vantage InFusion home automation controllers.
Python
5
star
24

loopj.github.com

Github pages repo for loopj.com
HTML
4
star
25

wii-trim-pcb-footprints

KiCad PCB outlines for creating PCB "shields" for trimmed Wii projects and Wii portables.
4
star
26

connect-redis-sse

Connect/Express middleware for publishing Server-Sent Events (EventSource) using Redis pub/sub.
JavaScript
4
star
27

tax-calculator

Ruby
3
star
28

resque-delayable

Resque plugin adds method rdelay to objects. Methods called with rdelay are executed asynchronously by resque, allowing your code to continue.
Ruby
3
star
29

jquery-simple-modal

Simple, unobtrusive modal dialog launcher
CoffeeScript
2
star
30

mongo-profiler

CoffeeScript
2
star
31

particle-cli-boilerplate

Template repo for new Particle projects, allowing you to build apps on the command line
Makefile
2
star
32

wii-breakout

Wii motherboard breakout board for USB, Bluetooth, GameCube controllers, A/V, and RST
2
star
33

manifest-generator

A cache.manifest generator in ruby, for use in your HTML5 offline apps.
Ruby
2
star
34

jekyll_layouts

Jekyll layouts shared between my various github pages
HTML
1
star
35

buggy-app

Ruby
1
star
36

androidtest

Java
1
star
37

unicodecheckmark.com

HTML
1
star
38

aws-instance-explorer

A way to visually explore your AWS instances in the browser
JavaScript
1
star
39

wii-homebrew-minimal

Makefile
1
star
40

wii-2d

C
1
star
41

wii-daughterboard

1
star