Hello all,

Bootstrap has lots of plugins that you can experiment with, and one of them is the Typeahead.js plugin similar to jQueryUI Autocomplete plugin. According to Bootstrap, Typeahead is an extendend plugin for quickly creating elegant typeaheads with any from text input. So given the description of the widget, I will provide a tutorial that integrates the plugin in an ASP.NET MVC project basing from this article Twitter Bootstrap Typeahead and ASP.NET MVC. The author from the source demonstrates preloaded country values but in my case, I modified it to handle searching through a database.

So to proceed with, just follow the steps below:

1. Create an ASP.NET MVC Project (VB).
2. Add an ADO.NET Entity model that connects to the AdventureWorks DB and it's CountryRegions table.
3. In your home controller, add the code that search countries based on a given value.
VB.NET Code:
  1. Private Shared _context As AdventureWorks2012Entities
  2.  
  3. ' GET: /Home
  4. Function Index() As ActionResult
  5.     Return View()
  6. End Function
  7.  
  8. <HttpGet>
  9. Public Function GetCountries(query As String) As JsonResult
  10.     _context = New AdventureWorks2012Entities()
  11.  
  12.     Dim result = (From country In _context.CountryRegions.AsEnumerable() Where country.Name.ToLower().Contains(query) Select country.Name)
  13.  
  14.     Return Json(result.ToArray(), JsonRequestBehavior.AllowGet)
  15. End Function

4. In the head section of Index View, add reference to typeaheadjs.css and bootstrap3-typeahead.js files.

HTML Code:
  1. <link href="~/Content/typeaheadjs.css" rel="stylesheet" type="text/css" />
  2. <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
  3. <script src="~/Scripts/jquery-1.10.2.min.js"></script>
  4. <script src="~/Scripts/bootstrap.min.js"></script>
  5. <script src="~/Scripts/bootstrap3-typeahead.js"></script>

5. Add the script to utilize the typeahead.js plugin for searching data.

JavaScript Code:
  1. <script type="text/javascript">
  2.  
  3.     $(document).ready(function () {
  4.  
  5.         $("#Countries").typeahead({
  6.             source: function (query, process) {
  7.                 var countries = [];
  8.                 map = {};
  9.  
  10.                 // This is going to make an HTTP get request to the controller
  11.                 return $.get('/Home/GetCountries', { query: query }, function (data) {
  12.  
  13.                     // Loop through and push to the array
  14.                     $.each(data, function (i, country) {
  15.                         map[country] = country;
  16.                         countries.push(country);
  17.                     });
  18.  
  19.                     // Process the details
  20.                     process(countries);
  21.                 });
  22.             },
  23.             updater: function (item) {
  24.                 var selectedCountry = map[item];
  25.  
  26.                 // Set the text to our selected id
  27.                 $("#details").text("Selected : " + selectedCountry);
  28.                 return item;
  29.             }
  30.         });
  31.     });
  32.  
  33. </script>

6. In the body section, just add a div element and an input element that will trigger the typeahead event.

HTML Code:
  1. <div id="details" class="row"></div>
  2. <div class="row">
  3.     <input type="text" id="Countries" class="form-control" placeholder="Counry Name" />
  4. </div>

Name:  Typeahead Example.png
Views: 2561
Size:  11.6 KB

That's it.. :-)


Regards,

KGComputers