inst/htmlwidgets/lib/typeahead.js/doc/jquery_typeahead.md

jQuery#typeahead

The UI component of typeahead.js is available as a jQuery plugin. It's responsible for rendering suggestions and handling DOM interactions.

Table of Contents

Features

Usage

API

jQuery#typeahead(options, [*datasets])

For a given input[type="text"], enables typeahead functionality. options is an options hash that's used for configuration. Refer to Options for more info regarding the available configs. Subsequent arguments (*datasets), are individual option hashes for datasets. For more details regarding datasets, refer to Datasets.

$('.typeahead').typeahead({
  minLength: 3,
  highlight: true
},
{
  name: 'my-dataset',
  source: mySource
});

jQuery#typeahead('val')

Returns the current value of the typeahead. The value is the text the user has entered into the input element.

var myVal = $('.typeahead').typeahead('val');

jQuery#typeahead('val', val)

Sets the value of the typeahead. This should be used in place of jQuery#val.

$('.typeahead').typeahead('val', myVal);

jQuery#typeahead('open')

Opens the suggestion menu.

$('.typeahead').typeahead('open');

jQuery#typeahead('close')

Closes the suggestion menu.

$('.typeahead').typeahead('close');

jQuery#typeahead('destroy')

Removes typeahead functionality and reverts the input element back to its original state.

$('.typeahead').typeahead('destroy');

jQuery.fn.typeahead.noConflict()

Returns a reference to the typeahead plugin and reverts jQuery.fn.typeahead to its previous value. Can be used to avoid naming collisions.

var typeahead = jQuery.fn.typeahead.noConflict();
jQuery.fn._typeahead = typeahead;

Options

When initializing a typeahead, there are a number of options you can configure.

Datasets

A typeahead is composed of one or more datasets. When an end-user modifies the value of a typeahead, each dataset will attempt to render suggestions for the new value.

For most use cases, one dataset should suffice. It's only in the scenario where you want rendered suggestions to be grouped based on some sort of categorical relationship that you'd need to use multiple datasets. For example, on twitter.com, the search typeahead groups results into recent searches, trends, and accounts – that would be a great use case for using multiple datasets.

Datasets can be configured using the following options.

Custom Events

The following events get triggered on the input element during the life-cycle of a typeahead.

Example usage:

$('.typeahead').bind('typeahead:select', function(ev, suggestion) {
  console.log('Selection: ' + suggestion);
});

NOTE: Every event does not supply the same arguments. See the event descriptions above for details on each event's argument list.

Class Names

To override any of these defaults, you can use the classNames option:

$('.typeahead').typeahead({
  classNames: {
    input: 'Typeahead-input',
    hint: 'Typeahead-hint',
    selectable: 'Typeahead-selectable'
  }
});


edwindj/d3rug documentation built on May 15, 2019, 11:04 p.m.