Bootstrap Learning Series using Bootstrap Typeahead components to achieve the Baidu Drop-down effect _javascript Skills

Source: Internet
Author: User

Underscore official website: http://underscorejs.org/

Reference Document: Http://www.css88.com/doc/underscore/

Page code:

@{
viewbag.title = "Index";
}
<script src= "Scripts/bootstrap-typeahead.js" ></script>
<script src= "Scripts/underscore-min.js" "></script>
<div>

Simple to use

<div style= "margin:10px 50px" >
<label for= "Product_search" >
Product Search:
</label >
<input id= "Product_search" type= "text" data-provide= "Typeahead" data-source= "[" DATA1 "," DATA2 "," DATA3 " ] '/>
</div>

Populating data with scripts

<div style= "margin:10px 50px" >
<label for= "Product_search" >
Product Search:
</label>
<input id= "Product_search_js" type= "text" data-provide= "Typeahead" >
</div>
<script Type= "Text/javascript" >
$ (document). Ready (function ($) {
$.fn.typeahead.constructor.prototype.blur = function () {
var = this;
settimeout (function () {that.hide ()},);
$ (' #product_search_js '). Typeahead ({
source:function (query, process) {return
["JS data 1", "JS Data 2", "JS data 3"];
},
highlighter:function (item) {return
"==>" + Item + "<==";
},
updater:function ( Item) {
Console.log ("" + Item + "' selected."); The browser console outputs
$ ("#product_search"). val (item);
return item;
}
})
</script>

Support for Ajax access to data

<div style= "margin:10px 50px" >
<label for= "Product_search" >
Product Search:
</label>
<input id= "Product_search_ajax" type= "text" data-provide= "Typeahead" >
</div>
<script Type= "Text/javascript" >
$ (' #product_search_ajax '). Typeahead ({
source:function (query, process) {
var parameter = {Query:query};
$.post (' @Url. Action ("Ajaxservice") ', parameter, function (data) {
process (data);}}
});
</script>

Working with Object data

<div style= "margin:10px 50px" >
<label for= "Product_search" >
Product Search:
</label>
<input id= "Product_search_object" type= "text" data-provide= "Typeahead" >
</div>
< Script type= "Text/javascript" >
$ (function () {
var products = [
{
id:0,
name: "Object1",
price:499.98
},
{
id:1,
name: "Object2",
price:134.99
},
{
id:2,
name: "Object3", Price
: 49.95
}
];
$ (' #product_search_object '). Typeahead ({
source:function (query, process) {
var results = _.map (Products, function (product) {return
product.name;
});
Process (results);
},
highlighter:function (item) {return
"==>" + Item + "<==";
},
Updater:function (item) {
Console.log ("' + Item +" ' selected. ");
return item;
}
}
); </script>

Controller

Public ActionResult Index ()
{return
View ();
}
Public ActionResult Ajaxservice ()
{
string query = "";
if (!string. Isnullorwhitespace (request["Query"))
query = request["Query"]. ToString ();
var data = ("Ajax1,ajax2,ajax3"). Split (', ');
return Json (data);

The effect chart shows the following:

Next continue to introduce bootstrap learning series of bootstrap Typeahead components to achieve Baidu Drop-down effect (continued)

The above is a small set to introduce the Bootstrap Learning series of the use of Bootstrap Typeahead components to achieve the effect of Baidu Drop-down, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.