对于bootstrap 2,有很多类型的ajax示例,例如这里twitter bootstrap typeahead ajax example。
但是我使用bootstrap 3并且找不到完整的示例,而是只有一堆不完整的信息片段,其中包含指向其他网站的链接,例如此处Where is the typeahead JavaScript module in Bootstrap 3 RC 1?
如果每次用户更改输入时,如果您通过ajax从服务器加载数据,有人可以发布一个完整的工作示例,说明如何使用带引导程序3的typeahead。
答案 0 :(得分:40)
使用bootstrap3-typeahead,我使用以下代码:
<input id="typeahead-input" type="text" data-provide="typeahead" />
<script type="text/javascript">
jQuery(document).ready(function() {
$('#typeahead-input').typeahead({
source: function (query, process) {
return $.get('search?q=' + query, function (data) {
return process(data.search_results);
});
}
});
})
</script>
后端在search
GET端点下提供搜索服务,在q
参数中接收查询,并返回格式为{ 'search_results': ['resultA', 'resultB', ... ] }
的JSON。 search_results
数组的元素显示在预先输入中。
答案 1 :(得分:4)
以下是我的灵感来自typeahead examples的一步一步体验,来自我们正在开发的Scala / PlayFramework应用。
在脚本LearnerNameTypeAhead.coffee
(convertible of course to JS)中,我有:
$ ->
learners = new Bloodhound(
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value")
queryTokenizer: Bloodhound.tokenizers.whitespace
remote: "/learner/namelike?nameLikeStr=%QUERY"
)
learners.initialize()
$("#firstName").typeahead
minLength: 3
hint: true
highlight:true
,
name: "learners"
displayKey: "value"
source: learners.ttAdapter()
我在页面上包含了typeahead包和我的脚本,输入字段周围有一个div
,如下所示:
<script src=@routes.Assets.at("javascripts/typeahead.bundle.js")></script>
<script src=@routes.Assets.at("javascripts/LearnerNameTypeAhead.js") type="text/javascript" ></script>
<div>
<input name="firstName" id="firstName" class="typeahead" placeholder="First Name" value="@firstName">
</div>
结果是,对于在第一个minLength(3)字符之后输入字段中键入的每个字符,页面发出一个GET请求,其URL类似于/learner/namelike?nameLikeStr=
加上当前键入的字符。服务器代码返回包含字段“id”和“value”的json对象数组,例如:
[ {
"id": "109",
"value": "Graham Jones"
},
{
"id": "5833",
"value": "Hezekiah Jones"
} ]
对于游戏,我需要路线文件中的内容:
GET /learner/namelike controllers.Learners.namesLike(nameLikeStr:String)
最后,我在一个新的typeahead.css文件中为下拉列表设置了一些样式,该文件包含在页面的<head>
(或可访问的.css)中
.tt-dropdown-menu {
width: 252px;
margin-top: 12px;
padding: 8px 0;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0, 0, 0, 0.2);
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
-webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
-moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
box-shadow: 0 5px 10px rgba(0,0,0,.2);
}
.typeahead {
background-color: #fff;
}
.typeahead:focus {
border: 2px solid #0097cf;
}
.tt-query {
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}
.tt-hint {
color: #999
}
.tt-suggestion {
padding: 3px 20px;
font-size: 18px;
line-height: 24px;
}
.tt-suggestion.tt-cursor {
color: #fff;
background-color: #0097cf;
}
.tt-suggestion p {
margin: 0;
}
答案 2 :(得分:3)
我正在使用它 https://github.com/biggora/bootstrap-ajax-typeahead
使用Codeigniter / PHP的代码的结果
<pre>
$("#produto").typeahead({
onSelect: function(item) {
console.log(item);
getProductInfs(item);
},
ajax: {
url: path + 'produto/getProdName/',
timeout: 500,
displayField: "concat",
valueField: "idproduto",
triggerLength: 1,
method: "post",
dataType: "JSON",
preDispatch: function (query) {
showLoadingMask(true);
return {
search: query
}
},
preProcess: function (data) {
if (data.success === false) {
return false;
}else{
return data;
}
}
}
});
</pre>
答案 3 :(得分:1)
您可以在此处找到有关如何升级到v3的信息:http://tosbourn.com/2013/08/javascript/upgrading-from-bootstraps-typeahead-to-typeahead-js/
答案 4 :(得分:-1)
<input id="typeahead-input" type="text" data-provide="typeahead" />
<script type="text/javascript">
var data = ["Aamir", "Amol", "Ayesh", "Sameera", "Sumera", "Kajol", "Kamal",
"Akash", "Robin", "Roshan", "Aryan"];
$(function() {
$('#typeahead-input').typeahead({
source: function (query, process) {
process(data);
});
}
});
});
</script>