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

下载地址

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

使用环境

  • Twitter Bootstrap 2.0+
  • jQuery 1.7+

页面准备

<link href="/path/to/bootstrap.css" rel="stylesheet">
<script src="/path/to/jquery.js" type="text/javascript"></script>
<script src="/path/to/bootstrap-typeahead.js" type="text/javascript"></script>

脚本

$(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
{url: null,timeout: 300,method: 'post',triggerLength: 3,loadingClass: null,displayField: null,preDispatch: null,preProcess: null
}
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 '<li><a href="#"></a></li>' The HTML rendering for a result item.
items integer 8 The maximum number of items to show in the results.
menu string '<ul class="typeahead dropdown-menu"></ul>' 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

var mySource = [{ id: 1, name: 'Terry'}, { id: 2, name: 'Mark'}, { id: 3, name: 'Jacob'}];$('#myElement').typeahead({source: mySource
});

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

$('#myElement').typeahead({ajax: '/path/to/mySource'
});

使用 Ajax

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

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

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

转载于:https://www.cnblogs.com/haogj/p/3378508.html

使用升级版的 Bootstrap typeahead v1.2.2相关推荐

  1. Bootstrap typeahead自动补全插件的坑

    ##Bootstrap typeahead 插件的坑 typeahead 自动补全插件, 还是先简单记录一下,如何使用吧: <div class="form-group"&g ...

  2. Bootstrap typeahead使用问题记录及解决方案

    简单介绍 Bootstrap typeahead插件是用来完成输入框的自动完成.模糊搜索和建议提示的功能,支持ajax数据加载,类似于jquery的流行插件Autocomplete. typeahea ...

  3. 第五章 使用 Bootstrap Typeahead 组件(百度下拉效果)

    推荐链接:http://www.cnblogs.com/haogj/p/3376874.html UnderScore官网:http://underscorejs.org/ 参考文档:http://w ...

  4. bootstrap typeahead实现模糊查询功能

    一:下载并引用 <script type="text/javascript" src="assets/js/bootstrap-typeahead.js" ...

  5. Bootstrap 3 Typeahead

    本文转自:https://github.com/tcrosen/twitter-bootstrap-typeahead 使用升级版的 Bootstrap typeahead  v1.2.2 http: ...

  6. 【Bootstrap】 typeahead自动补全

    typeahead 这篇文章记录了我在使用typeahead的一些问题,不是很全,但是基本够用. Bootstrap提供typeahead组件来完成自动补全功能. 两种用法: 直接给标签添加属性 &l ...

  7. Bootstrap相关优质项目必备网址

    1:文档全集:这里收集了Bootstrap从V1.0.0版本到现在,整个文档的历史.Bootstrap本身就是一个传奇,而这些文档就是传奇的见证! 官方网址:docs.bootcss.com/Boot ...

  8. 虚拟背景背后的技术:直播时各种背景是怎么实现的?

    简介:2020 年一场突如其来的疫情闯进了我们的生活,上网课.在家办公逐渐成为了一种常态,为了更好地保护用户隐私.提升用户体验,各大在线办公软件相继推出了虚拟背景功能,营造学习.办公氛围的同时,又能避 ...

  9. pod实例数是什么意思_[灌水] Kubernetes In Action: Pod

    Pod pod 可以有多个 副本,比如 slave 实际上是 pod 的副本,单一的 pod 存在唯一的 node 上. IP 分配的单位, pod 容器共享 pod 容器共享 volume, pod ...

  10. php ttf 字体 url,window_TTF字体文件如何安装 TTF文件打不开解决方法,TTF文件是Windows系统下的字体文 - phpStudy...

    TTF字体文件如何安装 TTF文件打不开解决方法 TTF文件是Windows系统下的字体文件,可通过系统自带的字体安装程序直接安装添加字体,比如常用的"仿宋_GB2312"字体就可 ...

最新文章

  1. 2.27 MapReduce Shuffle过程如何在Job中进行设置
  2. 嘿嘿,我就知道面试官接下来要问我 ConcurrentHashMap 底层原理了,看我怎么秀他...
  3. pandas使用dt.day_name函数从dataframe(Series)中的日期数据列中抽取日期对应的星期信息生成新的数据列(Monday、Sunday)
  4. 02 - Unit07:显示笔记下拉菜单、笔记的分享功能、笔记的删除功能
  5. linux导出硬件信息,Linux 上生成硬件信息与配置报告: Sysreport
  6. mysql count 不等于_Mysql 不同的 count 区别
  7. 名为 cursor_jinserted 的游标不存在_质量工程师必须了解的测量常识,你不知道怎么行...
  8. HTML-DOM零碎
  9. listview选中高亮
  10. el表达式的转义字符。
  11. Java 又双叒叕发布新版本,这么多版本如何灵活管理?
  12. java 摄像头_javacv调用摄像头拍照
  13. Freyja2版本对分库分表的处理方式
  14. DD-WRT安装与配置
  15. TortoiseSVN安装使用教程(超详细)
  16. 流水线计算公式及吞吐量
  17. C# 读写xml、excel、word、ppt、access
  18. 高精度加法(C++,高精度)
  19. 从单机到2000万 QPS 并发的 Redis 高性能缓存实践之路
  20. 武汉地铁站点最短路径搜索的实现(一)——Dijkstra算法(资料收集)

热门文章

  1. python 迭代器的方法_python--魔法方法,属性和迭代器
  2. python输出重定向_使用Python子进程重定向标准输出到标准输入...
  3. 【BZOJ4260】Codechef REBXOR(前i个数的最大区间异或值---01字典树+dp)
  4. 算法详解_【MOT】详解SORT与卡尔曼滤波算法
  5. xamarin android 设备,【Xamarin.Android】在Visual Studio中增强设备日志记录
  6. 线性代数 : 方程组的几何解释
  7. 比赛结束(第二届易观算法大赛)
  8. C/C++[codeup 2080]整数奇偶排序
  9. C/C++[codeup 2003, 2056]图形输出
  10. 自动驾驶 12-1: 实践中的状态估计 State Estimation in Practice