Maison base de données tutoriel mysql 使用升级版的 Bootstrap typeahead v1.2.2

使用升级版的 Bootstrap typeahead v1.2.2

Jun 07, 2016 pm 03:39 PM
bootstrap 使用

上次介绍了 Bootstrap 2 中附带的 typeahead,功能强大,但是使用起来不太方便,作者 Terry Rosen 已经升级了一个新版本 v1.2.2,作出了很大的改进。 下载地址 https://github.com/tcrosen/twitter-bootstrap-typeahead 使用环境 Twitter Bootstrap 2.0 jQue

上次介绍了 Bootstrap 2 中附带的 typeahead,功能强大,但是使用起来不太方便,作者 Terry Rosen 已经升级了一个新版本 v1.2.2,作出了很大的改进。

下载地址

https://github.com/tcrosen/twitter-bootstrap-typeahead

使用环境

  • Twitter Bootstrap 2.0+
  • jQuery 1.7+

页面准备

<span><span>link </span><span>href</span><span>="/path/to/bootstrap.css"</span><span> rel</span><span>="stylesheet"</span><span>&gt;</span>
<span><span>script </span><span>src</span><span>="/path/to/jquery.js"</span><span> type</span><span>="text/javascript"</span><span>&gt;</span><span>script</span><span>&gt;</span>
<span><span>script </span><span>src</span><span>="/path/to/bootstrap-typeahead.js"</span><span> type</span><span>="text/javascript"</span><span>&gt;</span><span>script</span><span>&gt;</span></span></span></span>
Copier après la connexion

脚本

$(myElement).typeahead(options);
Copier après la connexion

事件

事件 说明
grepper Filters relevant results from the source.
highlighter Highlights any matching results in the list.
itemSelected 当选中一个项目时的回调函数.
  • item: 选中的 HTML 元素
  • val: *val* 属性的值
  • text: *display* 属性的值
lookup Determines if source is remote or local and initializes the search.
matcher Looks for a match between the query and a source item.
render Renders the list of results.
select Selects an item from the results list.
sorter 排序结果.

初始化参数

名称 类型 默认值 说明
ajax object
{
    url: null,
    timeout: 300,
    method: 'post',
    triggerLength: 3,
    loadingClass: null,
    displayField: null,
    preDispatch: null,
    preProcess: null
}
Copier après la connexion
The object required to use a remote datasource. 
See also: ajax as a string (below)
ajax string null Optionally, a simple URL may be used instead of the AJAX object. 
See also: ajax as an object (above)
display string 'name' The object property to match the query against and highlight in the results.
item string '
  • '
    The HTML rendering for a result item.
    items integer 8 The maximum number of items to show in the results.
    menu string '' The HTML rendering for the results list.
    source object [] The source to search against.
    val string 'id' The object property that is returned when an item is selected.

    基本使用

    如果使用本地数据的话直接使用 source

    <span>var</span> mySource = [{ id: 1, name: 'Terry'}, { id: 2, name: 'Mark'}, { id: 3, name: 'Jacob'<span>}];
    
    $(</span>'#myElement'<span>).typeahead({
        source: mySource
    });</span>
    Copier après la connexion

    如果使用 Ajax 的话,可以直接指定 url,注意,现在的版本要求必须使用对象形式的数据源,默认显示文本为对象的 name 属性,可以通过初始化参数的 display 配置,默认的标识属性为 id ,可以使用 val 进行配置。

    $('#myElement'<span>).typeahead({
        ajax: </span>'/path/to/mySource'<span>
    });</span>
    Copier après la connexion

     

    使用 Ajax

    使用升级版的 Bootstrap typeahead v1.2.2

    $(<span>function</span><span> () {
        $(</span>'#product_search'<span>).typeahead({
            ajax: {
                url: </span>'@Url.Action("AjaxService")'<span>,
                timeout: </span>300,                   <span>//</span><span> 延时</span>
                method: 'post'<span>,
                triggerLength: </span>3,               <span>//</span><span> 输入几个字符之后,开始请求</span>
                loadingClass: <span>null</span>,             <span>//</span> 加载数据时, 元素使用的样式类<span>
                preDispatch: </span><span>null</span><span>,        // 发出请求之前,调用的预处理方法
                preProcess: </span><span>null         // Ajax 请求完成之后,调用的后处理方法</span><span>
            },
            display: </span>"name",     <span>//</span><span> 默认的对象属性名称为 name 属性</span>
            val: "id",           <span>//</span><span> 默认的标识属性名称为 id 属性</span>
            items: 8,            <span>//</span><span> 最多显示项目数量</span>
            itemSelected: <span>function</span> (item, val, text) {      <span>//</span><span> 当选中一个项目的时候,回调函数</span>
    <span>            console.info(item);
            }
        });
    });</span>
    Copier après la connexion

    使用升级版的 Bootstrap typeahead v1.2.2

     如果我们需要在请求中,除了递进搜索的参数之外,添加额外的请求参数怎么办呢,可以通过 preDispatch 进行额外处理,需要注意的是,一定要返回一个对象,这个对象将用来使用 jQuery 的 Ajax 方法作为参数。

    使用升级版的 Bootstrap typeahead v1.2.2

     $(<span>function</span><span> () {
            $(</span>'#product_search'<span>).typeahead({
                ajax: {
                    url: </span>'@Url.Action("AjaxService")'<span>,
                    timeout: </span>300,                   <span>//</span><span> 延时</span>
                    method: 'post'<span>,
                    triggerLength: </span>3,               <span>//</span><span> 输入几个字符之后,开始请求</span>
                    loadingClass: <span>null</span>,             <span>//
    </span>                preDispatch: <span>function</span><span> (query) {
                        </span><span>var</span> para = { other: 'xxxxxxxxx'<span> };
                        para.query </span>=<span> query;
                        </span><span>return</span><span> para;
                    },
    
                    preProcess: </span><span>function</span><span> (result) {
                        </span><span>return</span><span> result;
                    }
                },
                display: </span>"name",     <span>//</span><span> 默认的对象属性名称为 name 属性</span>
                val: "id",           <span>//</span><span> 默认的标识属性名称为 id 属性</span>
                items: 8,            <span>//</span><span> 最多显示项目数量</span>
                itemSelected: <span>function</span> (item, val, text) {      <span>//</span><span> 当选中一个项目的时候,回调函数</span>
    <span>                console.info(item);
                    </span><span>//</span><span> console.info($("#product_search").val());</span>
    <span>
                }
            });
        });</span>
    Copier après la connexion

    使用升级版的 Bootstrap typeahead v1.2.2

    Déclaration de ce site Web
    Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

    Article chaud

    Combien de temps faut-il pour battre Split Fiction?
    3 Il y a quelques semaines By DDD
    Repo: Comment relancer ses coéquipiers
    3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: Comment obtenir des graines géantes
    3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
    1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

    Article chaud

    Combien de temps faut-il pour battre Split Fiction?
    3 Il y a quelques semaines By DDD
    Repo: Comment relancer ses coéquipiers
    3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: Comment obtenir des graines géantes
    3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
    1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

    Tags d'article chaud

    Bloc-notes++7.3.1

    Bloc-notes++7.3.1

    Éditeur de code facile à utiliser et gratuit

    SublimeText3 version chinoise

    SublimeText3 version chinoise

    Version chinoise, très simple à utiliser

    Envoyer Studio 13.0.1

    Envoyer Studio 13.0.1

    Puissant environnement de développement intégré PHP

    Dreamweaver CS6

    Dreamweaver CS6

    Outils de développement Web visuel

    SublimeText3 version Mac

    SublimeText3 version Mac

    Logiciel d'édition de code au niveau de Dieu (SublimeText3)

    Comment introduire le bootstrap dans Eclipse Comment introduire le bootstrap dans Eclipse Apr 05, 2024 am 02:30 AM

    Comment introduire le bootstrap dans Eclipse

    Comment utiliser NetEase Mailbox Master Comment utiliser NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

    Comment utiliser NetEase Mailbox Master

    Comment utiliser l'application Baidu Netdisk Comment utiliser l'application Baidu Netdisk Mar 27, 2024 pm 06:46 PM

    Comment utiliser l'application Baidu Netdisk

    Comment introduire une idée dans bootstrap Comment introduire une idée dans bootstrap Apr 05, 2024 am 02:33 AM

    Comment introduire une idée dans bootstrap

    Tutoriel BTCC : Comment lier et utiliser le portefeuille MetaMask sur l'échange BTCC ? Tutoriel BTCC : Comment lier et utiliser le portefeuille MetaMask sur l'échange BTCC ? Apr 26, 2024 am 09:40 AM

    Tutoriel BTCC : Comment lier et utiliser le portefeuille MetaMask sur l'échange BTCC ?

    750 000 rounds de bataille en tête-à-tête entre grands modèles, GPT-4 a remporté le championnat et Llama 3 s'est classé cinquième 750 000 rounds de bataille en tête-à-tête entre grands modèles, GPT-4 a remporté le championnat et Llama 3 s'est classé cinquième Apr 23, 2024 pm 03:28 PM

    750 000 rounds de bataille en tête-à-tête entre grands modèles, GPT-4 a remporté le championnat et Llama 3 s'est classé cinquième

    Comment lire les résultats du test d'effet de médiation bootstrap dans stata Comment lire les résultats du test d'effet de médiation bootstrap dans stata Apr 05, 2024 am 01:48 AM

    Comment lire les résultats du test d'effet de médiation bootstrap dans stata

    Quelle est la différence entre bootstrap et springboot Quelle est la différence entre bootstrap et springboot Apr 05, 2024 am 04:00 AM

    Quelle est la différence entre bootstrap et springboot

    See all articles