Twitter TypeAhead.js远程示例

时间:2015-07-05 22:45:17

标签: typeahead.js

我正在尝试进行远程类型提前功能。我的远程JSON应该如何构建?在下面的示例中,用户为每个项目都有一个单独的JSON文件。请问,我必须为我的数据创建数千个文件吗?

https://github.com/twitter/typeahead.js/tree/gh-pages/data/films/queries

以下是示例:

var bestPictures = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../data/films/post_1960.json',
  remote: {
    url: '../data/films/queries/%QUERY.json',
    wildcard: '%QUERY'
  }
});

$('#remote .typeahead').typeahead(null, {
  name: 'best-pictures',
  display: 'value',
  source: bestPictures
});

1 个答案:

答案 0 :(得分:0)

您可以动态地在后端创建JSON数据。 In PHP, e.g., you can use its json_encode function to achieve this.

如果您对路线有困难,可以更改路线。以下两者的行为相同(给定正确的配置) - the second is actually typeaheads default

../data/films/queries/{query}.json
../data/films/queries?q={query}

As for the JSON structure you should check out one of the examples you referenced.