javascript - Twitter Typeahead 自定义模板 - 让默认示例正常工作

标签 javascript twitter-bootstrap

我在获取此处找到的默认 Twitter typeahead 自定义模板时遇到问题:http://twitter.github.io/typeahead.js/examples/#custom-templates 我能够让默认演示正常工作。 这是我的js代码

jQuery(document).ready(function() {

var bestPictures = [
    {"year": "1996", "value": "Tom", "url": "http://example.com"},
    {"year": "1998", "value": "Tim", "url": "http://example.com"}
];


$('#custom-templates .typeahead').typeahead(null, {
  name: 'best-pictures',
  display: 'value',
  source: bestPictures,
  templates: {
    empty: [
      '<div class="empty-message">',
        'unable to find any Best Picture winners that match the current query',
      '</div>'
    ].join('\n'),
    suggestion: Handlebars.compile('<div><strong>{{value}}</strong> – {{year}}</div>')
  }
   });
});

我无法在他们获取 bestPictures 的位置找到该示例的源代码。我还安装了 Handlebars 。当我在搜索框中输入字母 t 时,我的控制台日志显示 typeahead.bundle.js 中的 this.source 不是函数在这一行:this.source(query,sync,async);

此外,我想在选择下拉选项后进行重定向,类似于

on('typeahead:selected', function(event, datum) {
    window.location = datum.url
});

最佳答案

尝试使用 Bloodhound ,返回对象 bestPictures 作为可在 .typeahead suggestions 函数访问的属性

jQuery(document).ready(function() {

  var bestPictures = [{
    "year": "1996",
    "value": "Tom",
    "url": "http://example.com"
  }, {
    "year": "1998",
    "value": "Tim",
    "url": "http://example.com"
  }];

  var pictures = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: $.map(bestPictures, function(d) {
      return {
        value: d.value,
        // pass `bestPictures` to `suggestion`
        suggest: d
      }
    })
  });

  pictures.initialize();

  $("#custom-templates .typeahead").typeahead(null, {
    name: "best-pictures",
    display: "value",
    source: pictures.ttAdapter(),
    templates: {
      notFound: [
        "<div class=empty-message>",
        "unable to find any Best Picture winners that match the current query",
        "</div>"
      ].join("\n"),
      suggestion: function(data) {
        // `data` : `suggest` property of object passed at `Bloodhound`
        return "<div><strong>" + data.suggest.value + "</strong>" 
               + data.suggest.year + "</div>"
      }
    }
  });
});
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js">
</script>

<div id="custom-templates">
  <input type="text" class="typeahead" placeholder="Best picture winners" />
</div>

关于javascript - Twitter Typeahead 自定义模板 - 让默认示例正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35074983/

相关文章:

html - Bootstrap 移动不工作

javascript - 在一行中设置键和变量

javascript - 整个 JS 都在一个全局变量中?

javascript - 在 Chrome 下 - Mootools 注入(inject)因 null 元素而失败

css - 导航栏中按钮之间的自举距离

javascript - 无法将 "transform"添加到 css 以具有自定义 Bootstrap 模态动画

javascript - 如何停止仅适用于 IE 8 的脚本

javascript - 在 Angular 中如何让代码在回调函数之后运行?

html - 在未折叠的导航栏中对齐按钮链接和阻止按钮

html - 在 Bootstrap 中创建 App Store 按钮