twitter typeahead Js
189 Flares 189 Flares ×

In this Tutorial I will explain How to Create a Simple Autocomplete search functionality using jQuery.

Look at the jQuery AutoComplete Demo

A quick Google search will return so many jQuery autocomplete libraries and plugins and in that jQuery UI Autocomplete library is popular and widely used library.

But in this tutorial I am using Twitter Typeahead.js plugin :  An open source autocomplete JavaScript library by Twitter.

It comes with so many features like

  • Display suggestions to user as they types.
  • Shows suggestions like Google Autocomplete Search
  • Works with hard coded Data and as well as remote data
  • Suggestions from multiple Data sets.
  • Supports customized templates for suggestions.

First we will start with basic implementation of jQuery autocomplete using Twitter Typeahead.js which uses single data set.

Look at the sample HTML Code

 

I created simple Textbox with class “Countries” and now we have to assign data to this Textbox.

Look at the below code.

 

‘local’ attribute is used for hard coded json data.

Twitter Typeahead js

Twitter Typeahead js

 

How this Twitter Typeahead.js will work:

Whenever you add data to the text box using typeahead object it will generate some dynamic html code for displaying suggestion lists (you can see this code by inspecting the textbox element).

Twitter Typeahead Working

Twitter Typeahead Working

 

And in the demo I added some styles like text box on focus blue color and suggestion hover blue color.

If you want to add your own styles.Add styles to following classes “tt-query, tt-hint, tt-dropdown-menu, tt-suggestion” which are generated by Twitter Typeahead.js library.

Multiple Data Sets Suggestion Using Twitter Typeahead.js :

Now I will show you how to implement multiple data set suggestions.

We have to search for city names from different countries. For instance US and Canada,Sample Json Data

 

In the above example I used hard coded json data (client side). In this example we retrieve json data from server side.

I saved above json data into two files US.json and Canada.json.

 

We have to use ‘prefetch’ attribute to retrieve json data from an URL.

And I used ‘header’ attribute to display country Name as a header for two Data sets.

Twitter Typeahead.js library provides one more attribute ‘footer’ to display footer name. I am not using ‘footer’ attribute. You can try it.

Here is the demo.

twitter typeahead Js

twitter typeahead Js

Actually this ‘prefetch’ will grab the data from json on pageload and then stores it in browser’s Local Storage. You can see the json data in local Storage.

Twitter Typeahead Prefetch

Twitter Typeahead Prefetch

 

Now I will explain how to customize suggestions using Twitter Typeahead.js

Twitter Typeahead.js Customized templates for suggestions:

I liked this feature very much we can easily customize suggestion list based upon our requirements.

Take an example of  a blog we will be having different categories, for instance  HTML5,JavaScript,CSS3. Now we are searching for a particular post by Post name or title.

In search suggestion box we have to show Title,Category and Small Description with some styles.Like Post Name in bold font,below that small description and Category in Italic font and right side of suggestion box.

Twitter Typeahead.js library provides ‘template’ attribute to define our own styles.And to use this templates we have to provide template ‘engine’. Typeahead.js comes with one default engine called “Hogan”.

Sample Json data which contains keys like tag,name and description

 

Now we have to pass this json data along with template

 

‘valueKey’ attriutes defines the search criteria. For this example we have to search by post name. so I given ‘valueKey’ as “name”.

I defined a custom template for suggestion box which displays tag,name and description. We have to use {{}} to display Json Data. and I used Hogan template engine.

And HTML Code

 

We have to add Hogan.js library in header part along with jQuery,typeahead JavaScript libraries.

Now we have to apply style to this template.

 

Here is the Final jQuery AutoComplete Demo

But the problem with the prefetch attribute is it will save the entire Json data into the browser’s Local Storage which is not the feasible solution for sites like Twitter and Google. i.e., you have to return results from the server as-you-type.

Twitter Typeahead.js provides ‘remote’ attribute in which we can pass a search string as query parameter to server to return results from Database as-we-type.

In my next post I will explain about this ‘remote’ attribute.You may use PHP or Asp .net to retrieve data from server. I am familiar with Asp .net MVC. Go through jQuery Autocomplete using Asp Net MVC article to learn how retrieve data from server using Twitter Typeahead.js remote attribute.

Download the source Code for jQuery Autocomplete Tutorial

I hope you have enjoyed this article.If so share this post with your friends and also join our mailing list.Wait before leaving. why can’t you follow me on twitter or be a friend on Facebook to get in touch me.

And Finally Don’t Forgot to Join Our Reader List. Read Popular tutorial on HTML5

Author : Arunkumar Gudelli

I am a Simple Guy, Standing Infront of the World, asking you to Follow Me.
Don't miss any posts from Me,Subscribe to the RSS Feed.
You can connect me via @twitter or @facebook or Google+ or e-mail.

Loading Facebook Comments ...

  One Response to “A Simple jQuery Autocomplete Search Tutorial”

  1. It’s cool dude. :)

Leave a Reply

189 Flares Twitter 1 Facebook 2 Google+ 180 LinkedIn 6 StumbleUpon 0 Reddit 0 189 Flares ×