Home Database Mysql Tutorial 使用升级版的 Bootstrap typeahead v1.2.2

使用升级版的 Bootstrap typeahead v1.2.2

Jun 07, 2016 pm 03:39 PM
bootstrap use

上次介绍了 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>></span>
<span><span>script </span><span>src</span><span>="/path/to/jquery.js"</span><span> type</span><span>="text/javascript"</span><span>></span><span>script</span><span>></span>
<span><span>script </span><span>src</span><span>="/path/to/bootstrap-typeahead.js"</span><span> type</span><span>="text/javascript"</span><span>></span><span>script</span><span>></span></span></span></span>
Copy after login

脚本

$(myElement).typeahead(options);
Copy after login

事件

事件 说明
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
}
Copy after login
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>
    Copy after login

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

    $('#myElement'<span>).typeahead({
        ajax: </span>'/path/to/mySource'<span>
    });</span>
    Copy after login

     

    使用 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>
    Copy after login

    使用升级版的 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>
    Copy after login

    使用升级版的 Bootstrap typeahead v1.2.2

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1664
    14
    PHP Tutorial
    1267
    29
    C# Tutorial
    1239
    24
    How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

    How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

    How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

    Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

    How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

    Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

    How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

    There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

    How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

    There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

    How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

    To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

    How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

    To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

    How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

    How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

    See all articles