javascript - typeahead.js 提示和突出显示参数不适用于预取和远程

标签 javascript jquery typeahead.js

我按照此处的说明使用 bloodhound 实现 typeahead: http://twitter.github.io/typeahead.js/examples/#bloodhound

这是我的 html:

<div id="remote">
  <input class="typeahead" type="text" placeholder="Search for cast and directors"> 
</div>

这是我的js:

$(document).ready(function() {
var castDirectors = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../api/v1/search/people_typeahead',
  remote: '../api/v1/search/people_typeahead?q=%QUERY',
    dupDetector: function(remoteMatch, localMatch) {
        return remoteMatch.value === localMatch.value;
    }
});

castDirectors.initialize();

$('#remote .typeahead').typeahead(null, {
  name: 'cast-directors',
  displayKey: 'value',
  source: castDirectors.ttAdapter(),
    hint: false,
    highlight: true,
    templates: {
        empty: [
      '<div class="empty-message">',
      'No matching names',
      '</div>'
    ].join('\n'),
        suggestion: Handlebars.compile('<a id="typeahead" href="{{link}}"><p>{{value}}</p></a>')
    }       
});
});

但是,即使将提示设置为 false 并将突出显示设置为 true,我仍然会看到提示,并且不会在预输入中获得突出显示。我应该改变什么?

最佳答案

尝试放置提示,突出显示并添加 minLength: 1 而不是第一个 null,它应该如下所示:

$('#remote .typeahead').typeahead(
{
    hint: false,
    highlight: true,
    minLength: 1
},
{
  name: 'cast-directors',
  displayKey: 'value',
  source: castDirectors.ttAdapter(),

    templates: {
        empty: [
      '<div class="empty-message">',
      'No matching names',
      '</div>'
    ].join('\n'),
        suggestion: Handlebars.compile('<a id="typeahead" href="{{link}}"><p>{{value}}</p></a>')
    }       
});

关于javascript - typeahead.js 提示和突出显示参数不适用于预取和远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23587498/

相关文章:

php - 使用 jquery ajax post 提交表单输入数组

typeahead.js - Typeahead - 可滚动的下拉菜单

javascript - 通过 javascript ajast 获取 Url 源

javascript - 惰性求值在 Lodash 中不起作用

javascript - 如何将位置对齐到 3 个部分,上 - 中和下?

javascript - 如何让 Typeahead.js 使用对象的一个​​字段而不是全部字段自动完成或选择?

jquery - typeahead.js onselect 项目重定向到新窗口

javascript - 使用 ng-repeat AngularJS 构建大表后,Internet Explorer 中的垃圾收集需要很长时间

javascript - Prestashop 产品定制 - 保存到购物车

jquery选择器循环与使用函数each