首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Algolia类型提前检索结果

Algolia类型提前检索结果
EN

Stack Overflow用户
提问于 2015-12-09 15:08:24
回答 1查看 296关注 0票数 3

我试图在我的Rails应用程序中创建一个讨论工具。我想把用户添加到类似Facebook信使的讨论中。

为此,我实现了Algolia即时搜索sith Typeahead。它可以很好地显示结果,但是当我单击一个结果时,它什么也不做。看起来,如果我所使用的事件不起作用的话。

我不能用我的键盘上或下结果,这是非常奇怪的。你能帮我一下吗?我尝试了打字前文档中的所有事件,但没有人在工作。

非常感谢。

代码语言:javascript
复制
<div class="uk-grid message">
  <div class="uk-width-medium-2-6"></div>
  <div class="uk-width-medium-3-6 bubble">
    <%= form_tag create_discussion_path, method: :post, remote: true, class: 'uk-form', id: 'createDiscussionForm' do %>
        <div class="form-group">
          <div class="uk-grid">
            <div class="uk-width-1-6">
              <span><%= t('to') %></span>
            </div>
            <div class="uk-width-5-6">
              <%= text_field_tag 'recipients', '', class: 'recipients typeahead', id: 'typeahead-algolia-template', spellcheck: false %>
            </div>
          </div>
          <%= text_area_tag 'body', nil, cols: 3, class: 'form-control uk-width-1-1 body', placeholder: 'Ask something or just say hi!', required: true %>
        </div>
        <%= submit_tag t('dashboard.discussions.send_message'), class: 'uk-button uk-width-1-1' %>
    <% end %>
    <div class="arrow-right"></div>
  </div>
  <div class="uk-width-medium-1-6 text-center">
    <%= link_to user_path(current_user) do %>
        <%= image_tag(current_user.pictures.first.image(:square_thumb)) %>
    <% end %>
    <p><%= current_user.first_name %></p>
  </div>
</div>


<!-- Algolia search callback -->
<script type="text/javascript">
  function searchCallback(success, content) {
    if (content.query != $("#typeahead-algolia-template").val()) {
      // do not take out-dated answers into account
      return;
    }

    if (content.hits.length == 0) {
      // no results
      $('#hits').empty();
      return;
    }
  } // end search callback
</script>
<!-- end Algolia search callback -->

<!-- Algolia with typehead and hogan -->
<script type="text/javascript">
  $(document).ready(function() {

    // Algolia initialization
    var $inputfield = $("#typeahead-algolia-template");
    var algolia = algoliasearch('<%= ENV['ALGOLIA_ID'] %>', '<%= ENV['ALGOLIA_PUBLIC_KEY'] %>');
    var index = algolia.initIndex('<%= 'User_' + Rails.env  %>');
    var ttAdapterParams = {
      hitsPerPage: 5,
      facets: '*',
      tagFilters: ''
    };

    // Templating
    var templates = {
      algolia: {
        hit: Hogan.compile(
            '<div class="uk-grid">' +
            '<div class="uk-width-1-4">' +
            '<img src="{{{_highlightResult.picture.value}}}">' +
            '</div>' +
            '<div class="uk-width-3-4">' +
            '<span class="name">' +
            '{{{_highlightResult.first_name.value}}}, ' +
            '</span>' +
            '<span class="occupation">' +
            '{{{_highlightResult.occupation.value}}}' +
            '</span>' +
            '</div>' +
            '</div>'
        )
      }
    };


    // Search?
    $inputfield.typeahead({
          highlight: true,
          hint: true,
          minLength: 1
        },
        {
          source: index.ttAdapter(ttAdapterParams),
          displayKey: 'name',
          templates: {
            suggestion: function(hit) {
              var tpl = templates.algolia.hit.render(hit);
              //console.log(tpl, 'hit');
              return tpl;
            }
          }
        }).on('typeahead:open', function(event, data) {
//          var $form = $(this).closest('form');
//          $form.data('sendToServer', true);
//          $form.submit();
    });
    var val = $inputfield.typeahead('val');
    console.log(val);
    $("#typeahead-algolia-template").bind('typeahead:open', function(ev, suggestion) {
      console.log('Selection: ' + suggestion);
    });
  });
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-14 10:05:35

感谢伟大的阿尔戈利亚支持团队,我设法使它发挥作用!

正如建议的那样,我切换到了https://github.com/algolia/autocomplete.js,它非常完美。我改变了一些东西,基本上用.autocomplete和事件侦听器名称代替了.autocomplete。

下面是片段:

代码语言:javascript
复制
$inputfield.autocomplete({
      highlight: true,
      autoselect: true,
      openOnFocus: true
    }, [
      {
        source: $.fn.autocomplete.sources.hits(index, ttAdapterParams),
        displayKey: 'first_name',
        templates: {
          suggestion: function(hit) {
            var tpl = templates.algolia.hit.render(hit);
            //console.log(tpl, 'hit');
            return tpl;
          }
        }
      }]).on('autocomplete:selected', function(event, suggestion, dataset) {
        console.log(suggestion);
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34182177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档