2014-01-28 294 views
2

我有一个搜索功能的应用程序。索引页面显示数据库中的所有项目。另外,当我搜索我的产品时,它使用AJAX。如何将AJAX功能添加到我的分页中?我使用Kaminari作为我的分页。AJAX页面重新加载

  $(function() { 

       $("#name").autocomplete({ 
        source: "shirts/autocomplete", 
        autoFocus: false, 
        minLength: 1, 
        select: function(event,ui){ 
       document.getElementById("name").value = ui.item.value; 

        $.ajax({ 
       url:"shirts/show?name="+ui.item.value, 
       type:"GET", 
      });  
      } 
      }); 
      }); 
+0

它也取决于您的控制器。如果控制器的响应是整个页面,它将重新加载,无论如何。只需用数据回复JSON即可 –

回答

0

只需添加到您的分页链接remote: true或用JavaScript处理它,然后在你的控制器:

if request.xhr? 
    render json: @products # ajax request 
else 
    render layout: true # standard layout 
end 

你将不得不赶在你的JavaScript 'ajax:success'和追加收到产品。

+0

我在分页链接中添加了'remote:true',但它仍然无效。

<%= paginate @first, :remote => true %>
。然后在我的.js.erb文件中添加'$(“#container”)。html(“<%= escape_javascript(render”shirt/first“)%>”); ('#paginator“)。html('<%= escape_javascript(paginate(@first,:remote => true).to_s)%>');'。但是当我点击下一个时,它说内部服务器错误。 – Raghuveer

+0

检查您是否已正确通过kaminari分页[此处](http://stackoverflow.com/questions/9016943/multiple-pagination-with-kaminari-via-ajax)。如果问题不在那里,请粘贴您的服务器日志所说的内容(具体是500)。 –