使用 Rails 和 Ajax 更改返回的搜索结果数量

发布于 2024-11-07 08:49:12 字数 1483 浏览 6 评论 0原文

我最近在 Rails (2.3.4) 应用程序中使用 will_paginate 更改了分页,以使用 Ajax 进行分页和每页记录。分页是使用此处描述的方法完成的: http://github.com/mislav/will_paginate /wiki/Ajax-pagination

在我看来,我正在使用此代码:

Records per page: <%= select_tag :per_page, options_for_select([4,8,24,100,500], @per_page.to_i), :onchange => remote_function(:url => users_path, :method => :get, :with => "'per_page=' + this.getValue()") %>

如果我不查看搜索结果,这效果很好。但是,如果我进行搜索并且他们尝试更改每页记录,我的搜索结果将丢失并返回所有记录。我很确定这是由于我在 Remote_function 中调用的 url 造成的,但我不知道如何修复它。

这是我的控制器中的代码:

def index
@search = User.search(params[:search])
@search.order||="ascend_by_last_name"
if @search.count > 0
  @users = @search.paginate(:page => params[:page], :per_page => users_per_page )
  respond_to do |format|
    format.html # index.html.erb
    format.xml { render :xml => @users }
    format.csv { send_data @users.to_csv }
    format.js {
          render :update do |page|
            # 'page.replace' will replace full "results" block...works for this example
            # 'page.replace_html' will replace "results" inner html...useful elsewhere
            page.replace 'results', :partial => 'userview'
          end
      }      
    end
else
  flash[:notice] = "Sorry, your search didn't return any results."
  redirect_to users_path
end

end

任何帮助将不胜感激!谢谢。

I recently changed the pagination with will_paginate in my Rails (2.3.4) app to use Ajax for the pagination and records per page. The pagination was done using the method described here: http://github.com/mislav/will_paginate/wiki/Ajax-pagination

I'm using this code in my view:

Records per page: <%= select_tag :per_page, options_for_select([4,8,24,100,500], @per_page.to_i), :onchange => remote_function(:url => users_path, :method => :get, :with => "'per_page=' + this.getValue()") %>

This works fine if I'm not viewing search results. But if I do a search and them attempt to change the records-per-page, my search results are lost and all records are returned. I'm pretty sure this is due to the url I'm calling in my remote_function, but I don't know how to fix it.

This is the code in my controller:

def index
@search = User.search(params[:search])
@search.order||="ascend_by_last_name"
if @search.count > 0
  @users = @search.paginate(:page => params[:page], :per_page => users_per_page )
  respond_to do |format|
    format.html # index.html.erb
    format.xml { render :xml => @users }
    format.csv { send_data @users.to_csv }
    format.js {
          render :update do |page|
            # 'page.replace' will replace full "results" block...works for this example
            # 'page.replace_html' will replace "results" inner html...useful elsewhere
            page.replace 'results', :partial => 'userview'
          end
      }      
    end
else
  flash[:notice] = "Sorry, your search didn't return any results."
  redirect_to users_path
end

end

Any help would be appreciated! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-11-14 08:49:12

您可以将 per_page 参数附加到当前查询字符串的末尾。

:with => "'#{request.query_string}&per_page=' + this.getValue()"

这假设存在当前查询字符串,这可能会导致问题。

You can append the per_page param to the end of the current query string.

:with => "'#{request.query_string}&per_page=' + this.getValue()"

This assumes there is a current query string, which could cause issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文