Rails 中 Ajax 调用后分页不起作用

发布于 2024-12-13 11:21:16 字数 2115 浏览 1 评论 0原文

我正在使用 Kaminari 插件进行分页。我正在对我的应用程序进行 Ajaxifing,但遇到了问题。我有一个包含分页数据的表单以及用于过滤数据的表单(均通过 Ajax)。开始时分页效果很好,特定页面的 url 是“/admin/rooms?page=X”。在 ajax 过滤这些 url 后,将其更改为“/admin/rooms/filter?page=X”。 我可以指定分页时要触发的操作:

<%= paginate @rooms, :params => {:controller => 'rooms', :action => 'index'}, :remote => true %>

但随后变量 @rooms 会在索引中重置,并且我会再次对未过滤的数据进行分页。

我有什么想法可以解决它吗?

这是一些代码

  • rooms_controller.rb

    类 Admin::RoomsController <管理控制器
    定义索引
            @rooms = Room.all
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            @filter_values = [['房间号', 'room_number'], ['人数', 'person_count'], ['房间等级', 'room_class']]
    结尾
    
    定义过滤器
            案例参数[:filter_by]
            当“房间号”时
                    @rooms = Room.find(:all, :conditions => ["CAST(room_number AS VARCHAR) LIKE ?", "#{params[:filter_value]}%"])
            当'person_count'时
                    @rooms = Room.find(:all, :conditions => ["person_count LIKE ?", "#{params[:filter_value]}%"])
            别的
                    @rooms = Room.joins(:room_class).where("lower(class_name) LIKE '#{params[:filter_value]}%'")
            结尾
    
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            respond_to do |格式|
                    format.html { 重定向到 admin_rooms_path }
                    格式.js
            结尾
    结尾
    
  • index.html.erb

    <%= label_tag '过滤器:' %>
    <%= form_tag(filter_admin_rooms_path, :remote => true) 执行 %>
            <%= select_tag :filter_by, options_for_select(@filter_values) %>
            <%= text_field_tag :filter_value %>
            <%= Submit_tag '过滤器', :disable_with => '过滤..' %>
    <%结束%>
    
    ...
    
    
    <%= 渲染 'admin/rooms/shared/rooms_list' %>
  • _rooms_list.html.erb

    <%= 分页@rooms, :remote =>正确%>
    ...

I am using plugin Kaminari for pagination. I am Ajaxifing my application and I encountered a problem. I have a form with paginated data plus form for filtering data (both via Ajax). At start pagination works good, and the url's for particular pages are '/admin/rooms?page=X'. After ajax filtering those url's change for '/admin/rooms/filter?page=X'.
I can specify the action to trigger when paginating by:

<%= paginate @rooms, :params => {:controller => 'rooms', :action => 'index'}, :remote => true %>

but then variable @rooms gets resetted in index and I paginate unfiltered data again.

Any ideas how could I fix it?

Here's some code

  • rooms_controller.rb:

    class Admin::RoomsController < AdminController
    def index
            @rooms = Room.all
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            @filter_values = [['Room number', 'room_number'], ['Person count', 'person_count'], ['Room class', 'room_class']]
    end
    
    def filter
            case params[:filter_by]
            when 'room_number'
                    @rooms = Room.find(:all, :conditions => ["CAST(room_number AS VARCHAR) LIKE ?", "#{params[:filter_value]}%"])
            when 'person_count'
                    @rooms = Room.find(:all, :conditions => ["person_count LIKE ?", "#{params[:filter_value]}%"])
            else
                    @rooms = Room.joins(:room_class).where("lower(class_name) LIKE '#{params[:filter_value]}%'")
            end
    
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            respond_to do |format|
                    format.html { redirect_to admin_rooms_path }
                    format.js
            end
    end
    
  • index.html.erb:

    <%= label_tag 'Filter:  ' %>
    <%= form_tag(filter_admin_rooms_path, :remote => true) do %>
            <%= select_tag :filter_by, options_for_select(@filter_values) %>
            <%= text_field_tag :filter_value %>
            <%= submit_tag 'Filter', :disable_with => 'Filtering..' %>
    <% end %>
    
    ...
    
    <div id="rooms_list">
            <%= render 'admin/rooms/shared/rooms_list' %>
    </div>
    
  • _rooms_list.html.erb:

    <div id="paginator">
            <%= paginate @rooms, :remote => true %>
    </div>
    <table class="table_listing" style="width: 60%">
            ...
    </table>
    

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

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

发布评论

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

评论(1

同尘 2024-12-20 11:21:16

通过再次将参数传递给索引并强制分页路由到“rooms#index”,而不是当前页面(尽管不是 Ajax 的可见原因),问题得到了解决。

  • _rooms_list.html.erb

    <%= 分页@rooms, :params => {:控制器=> '房间', :action => '索引'}, :remote =>正确%>
    

参数通过 GET 从当前的 params 变量传递到索引。

  • rooms_controller.rb

    定义索引
            如果 params[:filter_value].nil?
                    @rooms = Room.all
            别的
                    @rooms = test_filter(params[:filter_value])
            结尾
    
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            @错误 = flash[:错误] || []
            @filter_values = [['房间号', 'room_number'], ['人数', 'person_count'], ['房间等级', 'room_class']]
    结尾
    
    定义过滤器
            案例参数[:filter_by]
            当“房间号”时
                    @rooms = Room.find(:all, :conditions => ["CAST(room_number AS VARCHAR) LIKE ?", "#{params[:filter_value]}%"])
            当'person_count'时
                    @rooms = Room.find(:all, :conditions => ["person_count LIKE ?", "#{params[:filter_value]}%"])
            别的
                    @rooms = Room.joins(:room_class).where("lower(class_name) LIKE '#{params[:filter_value]}%'")
            结尾
    
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            params[:authenticity_token] = nil
            参数[:提交] = nil
    
            respond_to do |格式|
                    format.html { 重定向到 admin_rooms_path }
                    格式.js
            结尾
    结尾
    
    def test_filter(val)
            rooms = Room.find(:all, :conditions => ["CAST(room_number AS VARCHAR) LIKE ?", "#{val}%"])
            房间
    结尾
    

test_filter当然是删除/重命名。我将一些我认为在分页时不应该在参数中传递到下一页的参数设为零。

Problem solved by passing parameters again to index and forcing paginate to route to 'rooms#index', not the current page (not visible cause of Ajax though).

  • _rooms_list.html.erb:

    <%= paginate @rooms, :params => {:controller => 'rooms', :action => 'index'}, :remote => true %>
    

Parametrs are passed to index by GET from the present params variable.

  • rooms_controller.rb:

    def index
            if params[:filter_value].nil?
                    @rooms = Room.all
            else
                    @rooms = test_filter(params[:filter_value])
            end
    
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            @errors = flash[:errors] || []
            @filter_values = [['Room number', 'room_number'], ['Person count', 'person_count'], ['Room class', 'room_class']]
    end
    
    def filter
            case params[:filter_by]
            when 'room_number'
                    @rooms = Room.find(:all, :conditions => ["CAST(room_number AS VARCHAR) LIKE ?", "#{params[:filter_value]}%"])
            when 'person_count'
                    @rooms = Room.find(:all, :conditions => ["person_count LIKE ?", "#{params[:filter_value]}%"])
            else
                    @rooms = Room.joins(:room_class).where("lower(class_name) LIKE '#{params[:filter_value]}%'")
            end
    
            @rooms = Kaminari.paginate_array(@rooms).page(params[:page]).per(5)
            params[:authenticity_token] = nil
            params[:commit] = nil
    
            respond_to do |format|
                    format.html { redirect_to admin_rooms_path }
                    format.js
            end
    end
    
    def test_filter(val)
            rooms = Room.find(:all, :conditions => ["CAST(room_number AS VARCHAR) LIKE ?", "#{val}%"])
            rooms
    end
    

test_filter is to remove/rename of course. I nil some parameter that I think shouldn't be passed in params to the next page when paginate.

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