【问题标题】:Typeahead autocomplete results not displayingTypeahead 自动完成结果未显示
【发布时间】:2016-01-03 22:12:08
【问题描述】:

我正在尝试使用库 typeahead.bundle.js(typeahead + bloodhound)。

这是我的代码:

  var engine = new Bloodhound({
    datumTokenizer: function(datum) {
      return Bloodhound.tokenizers.whitespace(datum.name);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
      url: "https://api.mapbox.com/geocoding/v5/mapbox.places/%QUERY.json?country=fr&access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6IlhHVkZmaW8ifQ.hAMX5hSW-QnTeRCMAy9A8Q",
      wildcard: "%QUERY",
      rateLimitWait: 1000,
      filter: function(response) {
        return $.map(response.features, function(city) {
          return {
            name: city.place_name,
            longitude: city.geometry.coordinates[0],
            latitude: city.geometry.coordinates[1]
          }
        });
      }
    }
  });

  var promise = engine.initialize();

  promise.done(function() { 
    $(".typeahead").typeahead({
      minLength: 2,
      highlight: true,
      hint: false
    }, 
    {
      displayKey: "name",
      source: engine.ttAdapter()
    });
  });

一切正常,但没有显示结果。

有人可以帮我吗?

谢谢!

【问题讨论】:

  • 无法满足请求。这是我尝试打开网址时得到的结果
  • 您的远程 URL 无效
  • 该网址正在运行...

标签: javascript autocomplete typeahead bloodhound


【解决方案1】:

cmets 是正确的,URL(或实际上是令牌)无效。使用另一个令牌,它运行良好,或者有点工作。出于美观的原因,我将响应过滤更改为更简单的

filter: function(response) {
  return response.features.map(function(city) {
    return {
      name: city.place_name,
        longitude: city.geometry.coordinates[0],
        latitude: city.geometry.coordinates[1]
      }
    })
  }
}

但主要问题是 this bug,这是众所周知的,令人惊讶的是,它从未在 github 存储库中得到更正(twitter.js 项目似乎或多或少已经死了) - 它是仅在使用寻血猎犬和远程资源时才会出现的问题。这是一个version with the corrections implemented - 现在它可以工作了 - 即使在小提琴中 -> http://jsfiddle.net/m2vLd2u4/

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多