使用升级版的 Bootstrap typeahead v1.2.2

php中文网
发布: 2016-06-07 15:39:15
原创
1129人浏览过

上次介绍了 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><span>link </span><span>href</span><span>="/path/to/bootstrap.css"</span><span> rel</span><span>="stylesheet"</span><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><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>
登录后复制

脚本

$(myElement).typeahead(options);
登录后复制

事件

事件 说明
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 @@######@@ 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

    {
        url: null,
        timeout: 300,
        method: 'post',
        triggerLength: 3,
        loadingClass: null,
        displayField: null,
        preDispatch: null,
        preProcess: null
    }
    登录后复制

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

    <span>var</span> mySource = [{ id: 1, name: 'Terry'}, { id: 2, name: 'Mark'}, { id: 3, name: 'Jacob'<span>}];
    
    $(</span>'#myElement'<span>).typeahead({
        source: mySource
    });</span>
    登录后复制

     

    使用 Ajax

    使用升级版的 Bootstrap typeahead v1.2.2

    $('#myElement'<span>).typeahead({
        ajax: </span>'/path/to/mySource'<span>
    });</span>
    登录后复制

    使用升级版的 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> 加载数据时, 元素使用的样式类<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>
    登录后复制

    使用升级版的 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>
    登录后复制
    最佳 Windows 性能的顶级免费优化软件
    最佳 Windows 性能的顶级免费优化软件

    每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

    下载
    相关标签:
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习
    PHP中文网抖音号
    发现有趣的

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号