仅当设置了类似的本地数组时,typeahead才会显示远程结果

时间:2019-04-16 17:03:54

标签: reactjs typeahead.js bloodhound

我已经正确设置了displayKey的所有内容,并且当我有本地数组但没有其他结果时,结果确实显示了。

componentDidMount () {
    this.placeInput = document.getElementById('place-input')
    this.bloodhound = new Bloodhound({
      name: "xxxxx",
      datumTokenizer: (d) => {
        return Bloodhound.tokenizers.whitespace(d.name);
      },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      prefetch: {
        url: 'http://api.geonames.org/searchJSON?username=xxxxx&featureClass=P&maxRows=5&name_startsWith=%QUERY'
      },
      //only seems to show results when this is present and those results have to be similar to 'California'
      // local: {
      //   name: 'California',
      //   title: 'California'
      // },
      remote: {
        url: "http://api.geonames.org/searchJSON?username=xxxxxx&featureClass=P&maxRows=5&name_startsWith=%QUERY",
        wildcard: '%QUERY',
      },
      limit: 10
    });
    this.bloodhound.initialize()
    $('#place-input').typeahead({
      hint: true,
      highlight: true,
      minLength: 2
    },
    {
      displayKey: 'name',
      source: this.bloodhound.ttAdapter()
    }).on('typeahead:selected', function(obj, datum) {
      console.log(datum)
    }.bind(this)); // END Instantiate the Typeahead UI
  }

应显示未设置本地数组的远程结果

0 个答案:

没有答案