Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight multiple words using Typeahead.js

I'm using typeahead.js for autocompletion, just like in this official example.

When I search for "rh" this results in "Rhode Island" (the matching characters are highligted).

When I search for "rh is" this results in "Rhode Island" (without any highlighting). How can I get this to work? Expected result: "Rhode Island".

P.S. If this requires modifications to the source code then this is OK for me.

like image 644
starshine wang Avatar asked Aug 20 '15 10:08

starshine wang


People also ask

How do I initialize a typeahead using the jQuery plugin?

When initializing a typeahead using the typeahead.js jQuery plugin , you pass the plugin method one or more datasets. The source of a dataset is responsible for computing a set of suggestions for a given query.

How to highlight search text in textarea or Div using JavaScript?

Using the following steps; you can highlight search text in Textarea or Div using javaScript; as shown below: First of all create a new html file and update the following code into your file: <p>JavaScript is a dynamic computer programming language.

How do I highlight the word fox in a jQuery file?

You can use the jquery highlight effect. But if you are interested in raw javascript code, take a look at what I got Simply copy paste into an HTML, open the file and click “highlight” – this should highlight the word “fox”. Performance wise I think this would do for small text and a single repetition (like you specified)

How do I create a type ahead query using jQuery?

The Basics. When initializing a typeahead using the typeahead.js jQuery plugin , you pass the plugin method one or more datasets. The source of a dataset is responsible for computing a set of suggestions for a given query.


1 Answers

The default integrated highlighting component of typeahead isn't smarth enough to fit your requirement.

I'm giving you an example with mark.js, a text highlighter for search terms/keywords or custom regular expressions. Please have a look at the website to learn more about the API.

DEMO JSFIDDLE

$(function() {
  // constructs the suggestion engine
  var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
    'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
    'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
    'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
    'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
    'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
    'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
    'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
    'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming', 'take bag'
  ];
  var states = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    // `states` is an array of state names defined in "The Basics"
    local: states
  });

  // Initialize typeahead with mark.js
  var $context = $("#bloodhound");
  $context.find(".typeahead").typeahead({
    hint: true,
    // disable integrated typeahead component
    highlight: false,
    minLength: 1
  }, {
    name: 'states',
    source: states
  }).on("typeahead:render", function() {
    // highlight matches with mark.js
    var searchTerm = $(this).val();
    $context.find(".tt-menu").mark(searchTerm);
  });
});
input {
  width: 250px;
  padding: 3px;
}

mark {
  background: yellow;
  color: black;
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdn.rawgit.com/julmot/mark.js/6.1.0/dist/jquery.mark.min.js"></script>
<script src="https://cdn.rawgit.com/twitter/typeahead.js/v0.11.1/dist/typeahead.bundle.min.js"></script>
<!-- Demo taken from http://twitter.github.io/typeahead.js/examples/ -->
<div id="bloodhound">
  <input class="typeahead" type="text" placeholder="States of USA. Type in 'rh is' or 'bag take'">
</div>

First, you will have to disable the default integrated highlighting component (disabled by default). Then, you'll have to listen for the renderer event and initialize mark.js on the suggestions of typeahead.

like image 195
dude Avatar answered Sep 29 '22 16:09

dude