Rails:在Algolia自动完成搜索中单击结果

扎奇

我安装了Algolia,以便为我的Rails应用程序实现高效:Algolia搜索栏配置合理,因为我可以在数据库中找到所需的内容。用户可以使用搜索栏中的自动完成功能来查找图钉。

但我想允许他们将结果显示在图钉页面上。

<!-- algolia search -->


 <input class="typeahead" type="text" placeholder="Search for users by name..." id="user-search" spellcheck="false" />

<!-- JQuery -->
<script src="//cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
<!-- Typahead.js is used to display the auto-completion menu -->
<script src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.10.4/typeahead.bundle.min.js"></script>
<!-- Hogan.js is used to render the hits using Mustache.js templating -->
<script src="//cdn.jsdelivr.net/hogan.js/3.0.0/hogan.common.js"></script>
<!-- Algolia -->
<script src="//cdn.jsdelivr.net/algoliasearch/latest/algoliasearch.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    // Replace the following values by your ApplicationID and ApiKey.
    var algolia = new AlgoliaSearch('MYAPPID', 'MYAPPPW');
    // replace YourIndexName by the name of the index you want to query.
    var index = algolia.initIndex('Pin');

    // Mustache templating by Hogan.js (http://mustache.github.io/)
    var template = Hogan.compile('<div class="hit">' +
      '<div class="name">' +
        '{{{ _highlightResult.description.value }}} ' +
        '({{{ _highlightResult.details.value }}})' +
      '</div>' +
      '</div>');

    // typeahead.js initialization
    $('#user-search').typeahead(null, {
      source: index.ttAdapter({ hitsPerPage: 5 }),
      displayKey: 'email',
      templates: {
        suggestion: function(hit) {
          // select matching attributes only
          hit.matchingAttributes = [];
          for (var attribute in hit._highlightResult) {
            if (attribute === 'name' || attribute == 'company') {
              // already handled by the template
              continue;
            }
            // all others attributes that are matching should be added in the matchingAttributes array
            // so we can display them in the dropdown menu. Non-matching attributes are skipped.
            if (hit._highlightResult[attribute].matchLevel !== 'none') {
              hit.matchingAttributes.push({ attribute: attribute, value: hit._highlightResult[attribute].value });
            }
          }

          // render the hit using Hogan.js
          return template.render(hit);
        }
      }
    });
  });
</script>


<!-- /algolia search -->

我希望在单击结果时将用户重定向到图钉显示视图。

所以我不知道如何将此链接呈现到Hogan链接中,因为我的图钉地址例如:myapp.com/pins/2代表图钉2

解决方案:将链接添加到hoogan模板中,其中slug对我来说是带有friendly_id gem的已修改URL。

// Mustache templating by Hogan.js (http://mustache.github.io/)
var template = Hogan.compile('<div class="hit">' +
'<a href="http://myapp.com/pins/{{{slug}}}" class="name">' +
    '{{{ _highlightResult.description.value }}} ' +
    '({{{ _highlightResult.details.value }}})' +
  '</a>' +
  '</div>');
希普

您必须修改hoogan模板以添加链接,这是使用linkedin链接的示例http://jsfiddle.net/bxvnqtan/

// Mustache templating by Hogan.js (http://mustache.github.io/)
var template = Hogan.compile('<div class="hit">' +
'<a href="{{{linkedin}}}" class="name">' +
    '{{{ _highlightResult.description.value }}} ' +
    '({{{ _highlightResult.details.value }}})' +
  '</a>' +
  '</div>');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章