php + Bootstrap-v3-Typeahead 自动完成组件的使用
Bootstrap 中的 Typeahead 组件就是通常所说的自动完成 AutoComplete,类似百度、谷歌等搜索提示;输入关键词出现相应的下拉列表数据。
是Bootstrap-3-Typeahead,不是Twitter open source的typeahead,两者用法有差异。外加如果配合原生的Bootstrap3 的话推荐还是用这个。(当然Twitter open source也有个bootstrap)。
1、基于Bootstrap v3 版本的 typeahead
第一,简单使用:
首先,最简单的使用方式,就是直接在标记中声明,通过 data-provide="typeahead" 来声明这是一个 typeahead 组件,通过 data-source= 来提供数据。当然了,你还必须提供 bootstrap-typeahead.js 脚本。
<html>
<head>
<meta charset="utf-8"/>
<link href="./bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script> <form id="searchform" class="navbar-form navbar-left" role="search" target="_blank" method="get" action="">
<div class="form-group">
<!--第一种方法-->
<input id="product_search" type="text" data-provide="typeahead" data-source='["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"]'>
</div>
<button type="submit" class="btn" id="searchbtn">搜索</button>
</form>
</body>
</html>
第二,支持 Ajax 获取数据
注意,我们提供了一个 source 函数来提供数据,这个函数接收两个参数,第一个参数 query 表示用户的输入,第二个参数是 process 函数,这个 process 函数是 typeahead 提供的,用来处理我们的数据。
如果你希望通过 Ajax 调用从服务器端获取匹配的数据,那么,在异步完成的处理函数中,你需要获取一个匹配的字符串数组,然后,将这个数组作为参数,调用 process 函数。
<html>
<head>
<meta charset="utf-8"/>
<link href="./bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script> <form id="searchform" class="navbar-form navbar-left" role="search" target="_blank" method="get" action="">
<div class="form-group">
<!--第一种方法-->
<!--<input id="product_search" type="text" data-provide="typeahead" data-source='["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"]'>-->
<input type="text" id="product_search" name="keys" class="form-control" data-provide="typeahead" data-value="" autocomplete="off" placeholder="请输入要搜索关键词">
</div>
<button type="submit" class="btn" id="searchbtn">搜索</button>
</form>
<script>
/***第二种方法****************************************/
console.log("欢迎使用typeahead");
/***第一种形式******返回json串***********************/
$('#product_search').typeahead({
source: function (query, process) {
return $.ajax({
url: './server.php',
type: 'post',
data: { query: query },
dataType: 'json',
success: function (result) {
var resultList = result.map(function (item) {
var aItem = { id: item.id, name: item.name };
return JSON.stringify(aItem);
});
return process(resultList); }
});
},
matcher: function (obj) {
var item = JSON.parse(obj);
return ~item.name.toLowerCase().indexOf(this.query.toLowerCase())
},
sorter: function (items) {
var beginswith = [], caseSensitive = [], caseInsensitive = [], item;
while (aItem = items.shift()) {
var item = JSON.parse(aItem);
if (!item.name.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(JSON.stringify(item));
else if (~item.name.indexOf(this.query)) caseSensitive.push(JSON.stringify(item));
else caseInsensitive.push(JSON.stringify(item));
}
return beginswith.concat(caseSensitive, caseInsensitive)
},
highlighter: function (obj) {
var item = JSON.parse(obj);
var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
return item.name.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong style="font-size:16px;">' + match + '</strong>'
})
},
updater: function (obj) {
var item = JSON.parse(obj);
$('#product_search').attr('data-value', item.id);
return item.name;
},
delay:500,
minLength:1,
items: 10, //显示10条
delay: 0, //延迟时间
}); /**第二种形式*****返回json串**********************************/
jQuery('#product_search').typeahead({
source: function (query, process) {
//query是输入值
jQuery.getJSON('./server.php', { "query": query }, function (data) {
process(data);
});
},
highlighter: function (item) {
var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong style="font-size:16px;">' + match + '</strong>'
})
},
updater: function (item) {
return item
},
afterSelect: function (item) {
//选择项之后的时间,item是当前选中的项
$("#product_search").attr("data-value",item.id);
},
delay:500,
minLength:1,
items: 10, //显示10条
delay: 0, //延迟时间
});
</script>
</body>
</html>
服务器处理文件 server.php
<?php
//1、 先获取当前搜索词"query"
//2、 从数据库中查询此字段的热词集合
//3、 从数据库中检索出包含此搜索词的热词集合,并按搜索次数排序,将数据返回给typeahead.js
if(!empty($_GET)){
$data = array();
$data[0]['id'] = 0;
$data[0]['name'] = "aaaaa";
$data[1]['id'] = 1;
$data[1]['name'] = "aaaaabbbbbb";
$data[2]['id'] = 2;
$data[2]['name'] = "aaaaaabbbbbccccc";
}else{
$data = array();
$data[0]['id'] = 0;
$data[0]['name'] = "baidu";
$data[1]['id'] = 1;
$data[1]['name'] = "baidu2";
$data[2]['id'] = 2;
$data[2]['name'] = "baidu3";
}
echo json_encode($data);die;
文件链接及打包地址:
ajax两种形式都支持:
ajax只支持第二种形式的 typeahead组件:
或 https://www.twitterbootstrapmvc.com/Documentation#typeahead 该 bootstrap.typeahead.min.js
demo打包地址:链接:http://pan.baidu.com/s/1geQ0DVX 密码:83vn
参考资料: 使用 Bootstrap Typeahead 组件 - 冠军 - 博客园
Bootstrap typeahead使用问题记录及解决方案
Bootstrap typeahead ajax result format - Example [结果格式的例子引导Typeahead Ajax] - 问题-字节技术
2、Twitter typeahead+bootstrap 官网用法:
typeahead的官方主页:http://twitter.github.io/typeahead.js/
typeahead的官方Example:http://twitter.github.io/typeahead.js/examples/
参考资料: 使用bootstrap typeahead插件
php + Bootstrap-v3-Typeahead 自动完成组件的使用的更多相关文章
- 【Bootstrap】 typeahead自动补全
typeahead 这篇文章记录了我在使用typeahead的一些问题,不是很全,但是基本够用. Bootstrap提供typeahead组件来完成自动补全功能. 两种用法: 直接给标签添加属性 &l ...
- bootstrap - typeahead自动补全插件
$('#Sale').typeahead({ ajax: { url: '@Url.Action("../Contract/GetSale")', //timeout: 300, ...
- Winform(C#.NET)自动更新组件的使用及部分功能实现
声明:核心功能的实现是由园子里圣殿骑士大哥写的,本人是基于他核心代码,按照自己需求进行修改的. 而AutoUpdaterService.xml文件生成工具是基于评论#215楼 ptangbao的代 ...
- 【Android】友盟的自动更新组件
前言 又好又专业的服务能帮开发者省很多时间.一开始做项目也准备自己来统计数据.自己做自动更新,随着使用友盟服务的时间增加,渐渐放弃了这种想法,转而研究如何更充分的使用,这里分享一下使用自动更新组件的心 ...
- 使用Bootstrap v3.3.4注意细节box-sizing
一.bootstrap样式 在Bootstrap v3.3.4中有下面一条重置样式: * { -webkit-box-sizing: border-box; -moz-box-sizing: bord ...
- jQuery UI Autocomplete是jQuery UI的自动完成组件(share)
官网:http://jqueryui.com/autocomplete/ 以下分享自:http://www.cnblogs.com/yuzhongwusan/archive/2012/06/04/25 ...
- Winform(C#.NET)自动更新组件的使用及部分功能实现(一点改进功能)
接前两篇继续: Winform(C#.NET)自动更新组件的使用及部分功能实现 Winform(C#.NET)自动更新组件的使用及部分功能实现(续) 借鉴文章:http://www.cnblogs.c ...
- Winform(C#.NET)自动更新组件的使用及部分功能实现(续)
接昨天的文章Winform(C#.NET)自动更新组件的使用及部分功能实现 强制更新的实现部分: 将DownloadConfirm窗体修改成单纯的类 public class DownloadConf ...
- 免费下载!Twitter Bootstrap V3 矢量界面素材
Bootstrap 3 Vector UI Kit 包含所有矢量格式的 Twitter Bootstrap 3 界面控制元素.Glyphicons 以及额外的一些界面素材,而且基本的图形元素都切好图了 ...
随机推荐
- spring security为不同用户显示各自的登录成功页面
一个常见的需求是,普通用户登录之后显示普通用户的工作台,管理员登陆之后显示后台管理页面.这个功能可以使用taglib解决. 其实只要在登录成功后的jsp页面中使用taglib判断当前用户拥有的权限进行 ...
- javascrpt 中的Ajax请求
回顾下javascript中的Ajax请求,写一个小例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- 4_STL设计理念_算法
STL算法,容器,迭代器的设计理念1.STL容器通过 类模板 技术,实现 数据类型 和 容器模型的分离:2.迭代器技术 实现了 遍历和操作容器的统一方法3.STL算法设计理念:通过预定义的函数对象和函 ...
- html本地服务器
html本地服务器 http://files.cnblogs.com/files/douxuyao/Aws.rar
- css中width的计算方式,以及width:100%的参考系
PS:测试浏览器均为chrome. 首先说下负margin的影响. 正常html页面在显示时,默认是根据文档流的形式显示的.文档流横向显示时,会有一个元素横向排列的基准线,并且以最高元素的vertic ...
- Java Debug调试简单方法--static使用
Public class Debug { public Debug() { } static void debugPrint(String src) { //System.out.print(src) ...
- App软件开发的完整在线流程(一看就懂)
App软件开发其实并不复杂,完整的在线App开发不过9个步骤,一看就明白到底是怎么回事. 第一步:需求评估 在App项目正式启动前,客户应该对自己的需求进行评估,确认项目的开发周期和成本.登录APIC ...
- lua module package.seeall选项
module 与 package.seeall http://blog.codingnow.com/2006/02/lua_51_module.html 使用 module("test&qu ...
- MVC4中重复使用JQuery Mobile Dialog的做法实践.
第一步:建立mobile项目类型 第二步:添加针对对话框的的DialogController.cs: 建立这个Controller的目的是此Dlg可以反复使用,把它做成一个固定界面,其他的Contro ...
- sql 里 text类型的操作(转载)
SQL Server中TEXT类型字段值在数据库中追加字符串方法 对text类型字段值进行追加更新的操作,一开始用了简单的update语句试了试,有错误,原来text.ntext类型的字段不能和 va ...