2016-12-07 24 views
0

我正在使用Rails创建我的应用程序,但实现了搜索的javascript。这是我的Algolia自动完成功能。我与Algolia一起参与免费('Hacker')计划,并且我想添加一个页脚。是否有捷径可寻?我在下面(我发现在:https://github.com/algolia/autocomplete.js/blob/master/README.md)不起作用。Algolia添加页脚到javascript自动完成搜索结果下拉列表

var client = algoliasearch("<%= ENV['ALGOLIA_API'] %>", "<%= ENV['ALGOLIA_SECRET'] %>"); 
var index = client.initIndex('User'); 

//initialize autocomplete on search input (ID selector must match) 
autocomplete('#aa-search-input', 
{ hint: false }, [{ 
    source: autocomplete.sources.hits(index, {hitsPerPage: 5}), 
    //value to be displayed in input control after user's suggestion selection 
    displayKey: function(suggestion) { return suggestion.first_name + " " + suggestion.last_name}, 
    //hash of templates used when rendering dataset 
templates: { 
     //'suggestion' templating function used to render a single suggestion 
     suggestion: function(suggestion) { 
      var link = "<form action='<%= ENV['HOST'] %>users/" + suggestion.id + "/friend_requests' method='post' id='addfriend' style='color: lightgreen'> <input name='authenticity_token' value='<%= form_authenticity_token %>' type='hidden'> <button type='submit' name='user_id' value='" + suggestion.id + "' class='btn-link'>Add</button></form>"; 
      var card = "<span>" + "<img src=<%= ENV['CLOUDINARY_SECOND_URL'] %>" + suggestion.photo.path + " alt='' class='avatar'> " + 
      suggestion._highlightResult.first_name.value + " " + suggestion._highlightResult.last_name.value + "</span><span>" + link +"</span>"; 

      return card 

     } 
    } 
    footer: '<span class="branding">Powered by <img src="https://www.algolia.com/assets/algolia128x40.png" /></span>' 
}]).on('autocomplete:selected', function(event, suggestion, dataset) { 
    var url = "<%= ENV['HOST'] %>users/"; 
    window.location.assign(url + suggestion.id)}); 
+0

通过“不工作”,我假设你的意思是:“页脚不出现”? – evadeflow

回答

0

你需要把footertemplates对象的内部。这样的:

templates: { 
    //'suggestion' templating function used to render a single suggestion 
    suggestion: function(suggestion) { 
     var link = "<form action='<%= ENV['HOST'] %>users/" + suggestion.id + "/friend_requests' method='post' id='addfriend' style='color: lightgreen'> <input name='authenticity_token' value='<%= form_authenticity_token %>' type='hidden'> <button type='submit' name='user_id' value='" + suggestion.id + "' class='btn-link'>Add</button></form>"; 
     var card = "<span>" + "<img src=<%= ENV['CLOUDINARY_SECOND_URL'] %>" + suggestion.photo.path + " alt='' class='avatar'> " + 
     suggestion._highlightResult.first_name.value + " " + suggestion._highlightResult.last_name.value + "</span><span>" + link +"</span>"; 

     return card 

    }, 
    footer: '<span class="branding">Powered by <img src="https://www.algolia.com/assets/algolia128x40.png" /></span>' 
} 

现在你拥有了它之外,因此它没有考虑footer模板考虑。

+0

非常感谢 - 这固定它(但不可见与safari,只是铬) – julianne