如何使用typeahead通配符

时间:2016-06-08 20:18:16

标签: twitter-bootstrap twitter typeahead bloodhound

我第一次尝试使用typeahead。我想根据用户输入更新url参数。外卡未被转换,值“QUERY”正被发送到远程服务器。

任何帮助都会受到赞赏:)

 myTypeahead = $('.typeahead').typeahead(
  {
    hint: true,
    highlight: true,
    minLength: 1
  },
  {
    name: 'addresses',
    source: addressResults = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.whitespace,
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      remote: {
              url:'https://urlpath/Search?query=%QUERY&country=GBR',            
              wildcard: '%QUERY',
              filter: function (data) {      
                  // Map the remote source JSON array to a JavaScript object array
                  return $.map(data.results, function(results,index) {  
                      return results.suggestion;
                  });
              },
              prepare: function (query, settings) {
                    settings.headers = {
                        'Auth-Token' : '1212'
                    };
                    return settings;
              }
       }
    })
  }
  ).on('keyup');

1 个答案:

答案 0 :(得分:7)

使用prepare时,需要手动处理通配符值。请参阅documentation for remote

例如:

 prepare: function(query, settings) {
    settings.url += '?q=' + query;
    return settings;
  },

这是associated fiddle