当查询不在displayField中时,引导Ajax Typeahead不显示结果
Bootstrap-Ajax-Typeahead not showing results when query not in displayField
我在Django应用程序上有一个Bootstrap Ajax Typeahead实现,该应用程序在我们的系统中搜索页面。我通过AJAX获得了与基于查询的搜索结果相匹配的页面,用户可以点击打字建议并转到有问题的页面。
但是,如果查询字符串不是displayField(title)的一部分,则建议不会显示,尽管我可以通过搜索机制看到它返回。
例如,如果我搜索"动态图片",我会通过搜索返回一个JSON对象,例如:
{
title: "Movies",
url: "/movies/",
description: "Find a motion picture in your area"
}
当在description属性中找到匹配时,我仍然希望向用户显示结果,但Typeahead只会在有匹配提示时显示。
如何显示查询不在displayField中的搜索结果?
注意:我使用的是typeahead bootstrap w/Bv3。
$(document).ready(function ($) {
var JSONSearchURL = "/search/";
var searchBox = $('#search-form');
searchBox.typeahead({
onSelect: function (item) {
window.location = item.value;
},
ajax: {
url: JSONSearchURL,
timeout: 800,
displayField: "title",
valueField: "url",
method: "get",
dataType: "JSON",
preDispatch: function (query) {
return {
q: query
}
},
preProcess: function (data) {
return data;
}
}
});
});
默认情况下,bootstrap-ajax-typeahead
使用displayField
来搜索匹配项。如果要覆盖此行为并搜索另一个字段,则需要覆盖grepper
事件。像这样的东西应该有效(未经测试):
grepper: function(data){
var that = this;
if(data && data.length){
items = $.grep(data, function(item){
return that.matcher(item['description']);
});
return items;
}
return null;
}
请在此处查看grepper
的默认实现:https://github.com/biggora/bootstrap-ajax-typeahead/blob/master/src/bootstrap-typeahead.js#L283
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在php文件中获取$.post-ajax传递的值
- Replacing $ .ajax?
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- Ajax发布表单序列化,发布引号'
- 通过Ajax将JavaScript函数传递给PHP文件
- ajax请求的顺序总是不同的
- 可以't使用Polymer's的核心ajax
- Ajax Live搜索发布到Laravel视图
- Ajax聊天消息重复而不仅仅是更新
- 从控制器返回后Ajax启动事件激发
- PHP AJAX图片上传示例不上传
- 从ajax请求中获取javascript对象
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 当查询不在displayField中时,引导Ajax Typeahead不显示结果
- Using typeahead.js with jquery ajax call
- 为什么typeahead.js不显示ajax响应中的所有项
- Bootstrap Typeahead AJAX
- 在typeahead.js源代码上传递ajax回调