Bootstrap 中的 Typeahead 组件就是通常所说的自动完成 AutoComplete,功能很强大,但是,使用上并不太方便。这里我们将介绍一下这个组件的使用。
相关推荐:《bootstrap基础教程》
第一,简单使用
首先,最简单的使用方式,就是直接在标记中声明,通过 data-provide="typeahead" 来声明这是一个 typeahead 组件,通过 data-source= 来提供数据。当然了,你还必须提供 bootstrap-typeahead.js 脚本。
<link> <div> <label>Product Search: </label> <input> </div> <script></script> <script></script>
第二,使用脚本填充数据
通常,我们使用脚本来填充数据,那么,页面可以变成如下的形式。
<link> <div> <label>Product Search: </label> <input> </div> <script></script> <script></script> <script> $(document).ready(function($) { // Workaround for bug in mouse item selection $.fn.typeahead.Constructor.prototype.blur = function() { var that = this; setTimeout(function () { that.hide() }, 250); }; $('#product_search').typeahead({ source: function(query, process) { return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"]; } }); }) </script>
注意,我们提供了一个 source 函数来提供数据,这个函数接收两个参数,第一个参数 query 表示用户的输入,第二个参数是 process 函数,这个 process 函数是 typeahead 提供的,用来处理我们的数据。
如果你希望通过 Ajax 调用从服务器端获取匹配的数据,那么,在异步完成的处理函数中,你需要获取一个匹配的字符串数组,然后,将这个数组作为参数,调用 process 函数。
第三,支持 Ajax 获取数据
说了半天,数据都是从本地获取的,到底如何从服务器端获取数据呢?
其实很简单,在 source 函数中,自己调用 Ajax 方法来获取数据,主要注意的是,在获取数据之后,调用 typeahead 的 process 函数处理即可。
$('#product_search').typeahead({ source: function (query, process) { var parameter = {query: query}; $.post('@Url.Action("AjaxService")', parameter, function (data) { process(data); }); } });
当然了,在服务器上,你需要创建一个服务来提供数据,这里,我们演示使用随机数来生成一组随机数据的方法。
public ActionResult AjaxService(string query) { System.Collections.ArrayList list = new System.Collections.ArrayList(); System.Random random = new Random(); for (int i = 0; i <h2 id="strong-第四-使用-em-highlighter-和-updater-em-strong"><strong>第四,使用 <em>highlighter 和 updater</em></strong></h2><p>除了使用 source 函数之外,还可以使用 <em>highlighter</em> 函数来特别处理匹配项目的显示,使用 updater 函数,在选择了某个匹配项之后,做出一些后继的处理。</p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/0a2e205cdeb05644ad7cd7d63b8354f6-1.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p>默认的 highlighter 是这样实现的,item 是匹配的项目,找到匹配的部分之后,使用 <strong> 加粗了。</strong></p><pre class="brush:php;toolbar:false">highlighter: function (item) { var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&') return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) { return '<strong>' + match + '</strong>' }) }
而 updater 的默认实现就更加简单了。
updater: function (item) { return item }
我们可以重写这两个函数,来实现自定义的处理。
<link> <div> <label>Product Search: </label> <input> </div> <script></script> <script></script> <script> $(document).ready(function($) { // Workaround for bug in mouse item selection $.fn.typeahead.Constructor.prototype.blur = function() { var that = this; setTimeout(function () { that.hide() }, 250); }; $('#product_search').typeahead({ source: function(query, process) { return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"]; }, highlighter: function(item) { return "==>" + item + "<=="; }, updater: function(item) { console.log("'" + item + "' selected."); return item; } }); }) </script>
第五,使用对象数据
实际上,你的数据可能是一组对象而不是一个字符串数组,下面的例子中,我们使用一个产品对象的数组来说明,每个产品对象有一个 id 编号,还有名称 name 和价格 price .
<link> <div> <label>Product Search: </label> <input> </div> <script></script> <script></script> <script></script> <script> $(document).ready(function ($) { // Workaround for bug in mouse item selection $.fn.typeahead.Constructor.prototype.blur = function () { var that = this; setTimeout(function () { that.hide() }, 250); }; var products = [ { id: 0, name: "Deluxe Bicycle", price: 499.98 }, { id: 1, name: "Super Deluxe Trampoline", price: 134.99 }, { id: 2, name: "Super Duper Scooter", price: 49.95 } ]; $('#product_search').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>
第六,高级用法
我们希望能够在提示中显示产品的更加详细的信息。
首先,修改我们的 source 函数,原来这个函数返回一个字符串的数组,现在我们返回一个产品 id 的数组,但是,process 函数期望得到一个字符串数组的参数,所以,我们将每个 id 都转换为字符串类型。
然后,typeahead 组件就会调用 matcher 函数来检查用户的输入是否与某个项目匹配,你可以使用产品的 id 在产品列表中获取产品对象,然后检查产品的名称与用户的输入是否匹配。
默认的 matcher 直接使用用户的输入来匹配,我们如果使用 id 的话,显然不能匹配,我们需要重写 matcher 函数。
matcher 接收一个当前项目的字符串,用户当前的输入为 this.query,匹配返回 true, 否则返回 false. 默认的 matcher 如下:
, matcher: function (item) { return ~item.toLowerCase().indexOf(this.query.toLowerCase()) }
将它重写为永远匹配,直接返回 true。而在 highlighter 中将显示结果替换为希望的产品名称和价格组合。在下一步的 highlighter 中,我们使用 Underscore 组件中的 find 方法,通过产品的 id 在产品列表中获取产品对象,然后,显示产品名称和价格的组合。
highlighter: function (id) { var product = _.find(products, function (p) { return p.id == id; }); return product.name + " ($" + product.price + ")"; }
默认的 updater 直接返回当前匹配的内容,我们这里是一个 id, 需要重写。
updater: function (item) { return item }
在用户选择之后,typeahead 将会调用 updater 函数,我们通过产品的 id 在产品列表中获取产品对象,然后
最后,updater 函数返回一个产品名称的字符串,为输入框提供内容。setSelectedProduct 是我们的一个自定义函数。
updater: function (id) { var product = _.find(products, function (p) { return p.id == id; }); that.setSelectedProduct(product); return product.name; }
下面是全部的代码。
<link> <div> <label>Product Search: </label> <input> <div></div> </div> <script></script> <script></script> <script></script> <script> $(document).ready(function ($) { // Workaround for bug in mouse item selection $.fn.typeahead.Constructor.prototype.blur = function () { var that = this; setTimeout(function () { that.hide() }, 250); }; var products = [ { id: 0, name: "Deluxe Bicycle", price: 499.98 }, { id: 1, name: "Super Deluxe Trampoline", price: 134.99 }, { id: 2, name: "Super Duper Scooter", price: 49.95 } ]; var that = this; $('#product_search').typeahead({ source: function (query, process) { $('#product').hide(); var results = _.map(products, function (product) { return product.id + ""; }); process(results); }, matcher: function (item) { return true; }, highlighter: function (id) { var product = _.find(products, function (p) { return p.id == id; }); return product.name + " ($" + product.price + ")"; }, updater: function (id) { var product = _.find(products, function (p) { return p.id == id; }); that.setSelectedProduct(product); return product.name; } }); $('#product').hide(); this.setSelectedProduct = function (product) { $('#product').html("Purchase: <strong>" + product.name + " ($" + product.price + ")").show(); } }) </script>
更多编程相关知识,请访问:编程入门!!
以上是Bootstrap中Typeahead元件的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Bootstrap加速了Web開發,通過提供預定義的樣式和組件,開發者可以快速搭建響應式網站。 1)它縮短了開發時間,例如在項目中幾天內完成基本佈局。 2)通過Sass變量和mixins,Bootstrap允許定製樣式以滿足特定需求。 3)使用CDN版本可以優化性能,提高加載速度。

Bootstrap是一個開源的前端框架,主要作用是幫助開發者快速構建響應式網站。 1)它提供了預定義的CSS類和JavaScript插件,方便實現複雜的UI效果。 2)Bootstrap的工作原理依賴於其CSS和JavaScript組件,通過媒體查詢實現響應式設計。 3)使用示例包括基本用法,如創建按鈕,以及高級用法,如自定義樣式。 4)常見錯誤包括類名拼寫錯誤和未正確引入文件,建議使用瀏覽器開發者工具調試。 5)性能優化可通過自定義構建工具實現,最佳實踐包括使用語義化HTML和Bootstrap的預定義

Bootstrap通過網格系統和媒體查詢實現響應式設計,使網站適應不同設備。 1.使用預定義類(如col-sm-6)定義列寬。 2.網格系統基於12列,需注意總和不超12。3.使用斷點(如sm、md、lg)定義不同屏幕尺寸下的佈局。

Bootstrap是一套開源的前端框架,用於快速開發響應式網站和應用。 1.它提供了響應式設計、一致的UI組件和快速開發的優勢。 2.網格系統使用flexbox佈局,基於12列結構,通過.container、.row和.col-sm-6等類實現。 3.自定義樣式可以通過修改SASS變量或覆蓋CSS實現。 4.常用JavaScript組件包括模態框、輪播圖和折疊。 5.優化性能可以通過只加載必要組件、使用CDN和壓縮合併文件來實現。

Bootstrap和JavaScript可以無縫整合,賦予網頁動態功能。 1)使用JavaScript操作Bootstrap組件,如模態框和導航欄。 2)確保jQuery正確加載,避免常見集成問題。 3)通過事件監聽和DOM操作實現複雜用戶交互和動態效果。

如何使用 Bootstrap 獲取搜索欄的值:確定搜索欄的 ID 或名稱。使用 JavaScript 獲取 DOM 元素。獲取元素的值。執行所需的操作。

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能