jQuery in XPages #8 – Tokeninput autocomplete

In these two articles I am going to illustrate and describe how to implement an alternative to the XPage typeAhead, using the “jQuery Tokeninput“. Unlike the XPages typeahead the jQuery Tokeninput has a large and flexible paramaterized API which allows for control over how the results are displayed, how the results are created and the source of the data. We are going to look at how to search a basic JSON string, a notes agent generated JSON feed, a twitter JSON feed, and an XPage REST JSON feed.

Tokeninput.js

Introduction
Tokeninput provides a typeAhead-like capability whereby a user is presented with matching options as they start to type into a text field. The source of the data can either be a local JSON object or a web provided JSON object.

Basic Tokeninput user interaction
Tokeninput with facebook look and feel
Tokeninput with facebook look and feel
Tokeninput with mac look and feel
Tokeninput with mac look and feel

The search results are determined from a JSON string and can come from any web source; flickr, twitter, facebook and domino to name but a few examples. What is really nice about Tokeninput is the control and flexibility provided to the developer out of the box allows you to make some very impressing, interactive webpages with very little effort. Unlike the XPage typeahead Tokeninput will also allow you to select multiple values and the display of these values can be controlled using a CSS.

Example Twitter feed with formatted results
Example Twitter feed with formatted results

Demonstration

The XPages integration of Tokeninput is demonstrated here

Download

The demonstration database can be downloaded from the link above or from here

How does it work in XPages?

For a more detailed dive into how the examples can be recreated and function in an XPage please check out the how does it work article

Tokeninput API

Tokeninput is extremely versatile and has a large number of parameters (as of v1.6) – these are taken straight from the website and you should check out all the examples on the website to get a good idea of all the capabilities

Search Settings

  • method
    • The HTTP method (eg. GET, POST) to use for the server request. default: “GET”.
  • queryParam
    • The name of the query param which you expect to contain the search term on the server-side. default: “q”.
  • searchDelay
    • The delay, in milliseconds, between the user finishing typing and the search being performed. default: 300 (demo).
  • minChars
    • The minimum number of characters the user must enter before a search is performed. default: 1 (demo).
  • propertyToSearch
    • The javascript/json object attribute to search. default: “name” (demo).
  • 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. default: 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. default: false.

Pre-population Settings

  • 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. default: null (demo).

Display Settings

  • hintText
    • The text to show in the dropdown label which appears when you first click in the search field. default: “Type in a search term” (demo).
  • noResultsText
    • The text to show in the dropdown label when no results are found which match the current query. default: “No results” (demo).
  • searchingText
    • The text to show in the dropdown label when a search is currently in progress. default: “Searching…” (demo).
  • 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. default: × (demo).
  • animateDropdown
    • Set this to false to disable animation of the dropdown default: true (demo).
  • theme
    • Set this to a string, eg “facebook” when including theme css files to set the css class suffix (demo).
  • 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 default: function(item){ return “<li>” + item.propertyToSearch + “</li>” } (demo).
  • 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. default: function(item){ return “<li><p>” + item.propertyToSearch + “</p></li>” } (demo).

Tokenization Settings

  • tokenLimit
    • The maximum number of results allowed to be selected by the user. Use null to allow unlimited selections. default: null (demo).
  • tokenDelimiter
    • The separator to use when sending the results back to the server. default: “,”.
  • preventDuplicates
    • Prevent user from selecting duplicate values by setting this to true. default: false (demo).
  • 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. default: id

Callbacks

  • 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. default: null (demo).
  • onAdd
    • A function to call whenever the user adds another token to their selections. defaut: null (demo).
  • onDelete
    • A function to call whenever the user removes a token from their selections. default: null (demo).
  • onReady
    • A function to call after initialization is done and the tokeninput is ready to use. default: null


Advertisements

5 thoughts on “jQuery in XPages #8 – Tokeninput autocomplete

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s