最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typeahead.js hint and highlight param not working with prefetch & remote - Stack Overflow

programmeradmin4浏览0评论

I followed the instruction here to implement typeahead with bloodhound: .js/examples/#bloodhound

This is my html:

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

This is my 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: Handlebarspile('<a id="typeahead" href="{{link}}"><p>{{value}}</p></a>')
    }       
});
});

however, even with hint set to false and highlight set to true, I'm still seeing hint and not getting highlights in the typeahead. What should I change?

I followed the instruction here to implement typeahead with bloodhound: http://twitter.github.io/typeahead.js/examples/#bloodhound

This is my html:

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

This is my 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>')
    }       
});
});

however, even with hint set to false and highlight set to true, I'm still seeing hint and not getting highlights in the typeahead. What should I change?

Share Improve this question edited May 14, 2014 at 5:05 Ian Lin asked May 10, 2014 at 23:46 Ian LinIan Lin 3841 gold badge5 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

Try to place the hint, highlight and add also minLength: 1 instead of the first null, it should look like this:

$('#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>')
    }       
});

Unfortunately the code for the remote example on the typeahead.js site doesn't use options in the call to the typeahead() function and a copy/paste will lead you into this problem.

As it was well pointed by prsnca you have to make sure to add these options in the first argument of the function.

No Highlight

$('#remote .typeahead').typeahead(null, {
  name: 'best-pictures',
  displayKey: 'value',
  source: bestPictures.ttAdapter()
});

Highlight

$('#remote .typeahead').typeahead(
  {
    hint: false,
    highlight: true,
    minLength: 1
  },
  {
    name: 'best-pictures',
    displayKey: 'value',
    source: bestPictures.ttAdapter()
  }
);
发布评论

评论列表(0)

  1. 暂无评论