【问题标题】:typeahead.js deduplicate between prefetch and remote datasourcestypeahead.js 在预取和远程数据源之间进行重复数据删除
【发布时间】:2014-06-25 09:17:24
【问题描述】:

我正在使用带有预取和远程的 typeahead.js http://twitter.github.io/typeahead.js/examples/#custom-templates

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

castDirectors.initialize();

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

但是,预取 JSON 和远程 JSON 中有重复的条目。如何进行重复数据删除,使其只显示一个条目?

【问题讨论】:

    标签: javascript jquery json duplicate-removal typeahead.js


    【解决方案1】:

    在您的 Bloodhound 初始化代码中添加 dupDector 选项,即将以下代码放在“remote:”之后:

    dupDetector: function(remoteMatch, localMatch) {
        return remoteMatch.value === localMatch.value;
    }
    

    您没有包含您的 JSON,因此我无法确定上面代码中的比较是否正确。此代码将忽略本地和远程数据源中的重复值。

    【讨论】:

    • 没问题@IanLin。很高兴我能帮忙:)
    • 这仍然是一个选项吗?除了旧的要点和 SO 帖子之外,我在任何地方都找不到它的文档。更新:这被识别取代:github.com/twitter/typeahead.js/issues/946
    • @PeterP。是的,我的回答是针对旧版本的 Bloodhound。感谢您提供有用的更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2013-09-23
    • 1970-01-01
    • 2022-08-23
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多