简体   繁体   English

使用Typeahead.js的预取显示总结果数

[英]Showing total results count using Typeahead.js's prefetch

I'm using Typeahead.js with an implementation that looks very similar to the "multiple datasets" found in the examples : 我正在使用Typeahead.js,其实现看起来与示例中的“多个数据集”非常相似:

var nbaTeams = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      prefetch: '../data/nba.json'
});

var nhlTeams = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      prefetch: '../data/nhl.json'
});

var footer = function (context) {
    // calculate total hits here
    return "<a href='...'>" + count + "</a>";
}

$('#multiple-datasets .typeahead').typeahead(null, {    
      name: 'nba-teams',
      display: 'team',
      source: nbaTeams,
      templates: {
          header: '<h3 class="league-name">NBA Teams</h3>'
      },
      limit: 3
    },
    {
      name: 'nhl-teams',
      display: 'team',
      source: nhlTeams,
      templates: {
          header: '<h3 class="league-name">NHL Teams</h3>',
          footer: footer
      },
      limit: 3
});

I'm using the latest version of Typeahead.js (v0.11.1). 我正在使用最新版本的Typeahead.js(v0.11.1)。 I'm trying to add a footer template to the bottom of the NHL teams section which has the total number of matching results. 我正在尝试将一个页脚模板添加到NHL团队部分的底部,该部分具有匹配结果的总数。 Something like <a href="...">Browse all ### results</a> . <a href="...">Browse all ### results</a> I can't find anywhere in the documentation where I can pull the count of total hits from Bloodhound. 我在文档中找不到任何可以从Bloodhound中获取总点击次数的地方。

I've seen people do this with remote data sources, but my data source is small enough to be pulled in and cached so I'd like to use prefetch. 我见过人们使用远程数据源来做这件事,但我的数据源很小,可以被拉入和缓存,所以我想使用预取。

I think your other code is perfectly fine, you just need to update your footer function with following. 我认为您的其他代码非常好,您只需要通过以下内容更新footer功能。

var footer = function (context) {
    // calculate total hits here
    return "<a href='#'>browse all <b>" + context.suggestions.length + "</b> results</a>";
}

Look at this fiddle. 看看这个小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM