【问题标题】:Algolia search: access results onclickAlgolia 搜索:onclick 访问结果
【发布时间】:2016-05-02 18:17:47
【问题描述】:

我将algolia search 用于我的rails 应用程序,我使用typehead.js 设置了自动完成方法。但是当我在 URL 中使用大写字母时,我无法正确重定向...这是我的代码:

    // 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">' +
  '<a href="http://yhumans.com/{{{twitter}}}">'
 +
  '<div class="small-text">' +
    '{{{ _highlightResult.name.value }}} ' +
  '</div>' +
  '<div class="small-text">' +
    '@' +
    '{{{ _highlightResult.twitter.value }}} ' +
  '</div>' +
  '</a>' +
  '</div>');


// typeahead.js initialization
$('#user-search').typeahead(null, {
  source: index.ttAdapter({ hitsPerPage: 5 }),

  displayKey: 'twitter',
  templates: {
    suggestion: function(hit) {

      // select matching attributes only
      hit.matchingAttributes = [];
      for (var attribute in hit._highlightResult) {
        if (attribute === 'name' || attribute == 'twitter') {
          // 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);

    }
  }
});

问题在于我用来让用户点击结果以访问页面的这行代码:

<a href="http://yhumans.com/{{{twitter}}}">'

事实上,我的一些用户在他们的推特用户名中使用了大写字母,所以我无法在搜索结果中正确重定向到他们的个人资料。

让我解释一下:http://yhumans.com/myname 是一个正确的 URL。 http://yhumans.com/MyName 是错误的 URL 我尝试为该变量使用小写字母:“twitter”。但我找不到正确的方法。

我知道一种方法是将 twitter 变量小写。但问题是功能“小写!”在 js 中似乎不起作用。

有什么想法吗?

【问题讨论】:

    标签: javascript ruby-on-rails search algolia


    【解决方案1】:

    首先,两个不会直接回答问题但相关的cmets:

    • 对于 Hogan 模板,当文本中不应包含 HTML 时应使用 {{variable}},应使用 {{{variable}}}。这就是为什么你应该使用{{twitter}}
    • Algolia 实际上已经将typeahead.js@0.10 分叉成autocomplete.js,你应该看看。

    话虽如此,你给出了一个解决方案,即使你是对的,.downcase! 不存在,它实际上是.toLowercase()
    在您的建议功能中,只需将 twitter 属性小写即可:

    function (hit) {
      hit.twitter = hit.twitter.toLowerCase();
      // ...
    }
    

    这种处理自动完成重定向的方式的一个问题是用户将无法使用他/她的键盘来选择结果。 autocomplete.jstypeahead.js 的推荐方法是分别使用 autocomplete:selectedtypeahead:selected 事件:

    $('#user-search').typeahead(/* ... */).on('typeahead:selected', function (ew, selection, dataset) {
      // Redirect to http://yhumans.com/my-twitter-name
      window.location.href = 'http://yhumans.com/' + selection.twitter;
    });
    

    要在用户悬停或使用箭头选择结果时向用户显示当前选择,您可以使用.aa-hint (autocomplete.js) 或.tt-hint (typeahead.js) 设置不同的背景颜色。

    【讨论】:

    • 是的,这很难。现在我只使用小写解决方案,即使有缺点 > 没有键盘命令,它也能很好地工作。我猜你回答了我的问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多