Visitar URL original
Pagination on table_for inside sections and panels · Issue #1116 · activeadmin/activeadmin · GitHub
Skip to content

Pagination on table_for inside sections and panels #1116

Closed

Description

Thanks everyone for AA, this is absolutely wicked!

Would love to see a way to implement a DRY pagination to table_for inside sections and panels on both the dashboard and in model show pages when displaying its children items

Sorry if this is already out there.

Activity

  1. huk commented on Mar 21, 2012

    @huk

    I have following code to do that

        panel :test do
          paginated_collection(my_collection.page(params[:page]).per(15), download_links: false) do
            table_for(collection, sortable: false) do
              column :start
              column :stop
              column :duration        
            end
          end
        end
  2. jbmyid commented on Nov 28, 2014

    @jbmyid

    Doesnt work with will_paginate, When I use above code it gives error as method "per" undefined for activerelation object.
    And if i used will_paginate ie: paginate(per_page: 10, page: 1) , it gives the error to use above code format.

  3. jbmyid commented on Nov 28, 2014

    @jbmyid

    @timoschilling

    Already did that but did not solve the problem,
    got work around by

    if defined?(WillPaginate)
      ActiveSupport.on_load :active_record do
        module WillPaginate
          module ActiveRecord
            module RelationMethods
              def per(value = nil) per_page(value) end
              def total_count() count end
            end
          end
          module CollectionMethods
            alias_method :num_pages, :total_pages
          end
        end
      end
    end
  4. timoschilling commented on Nov 28, 2014

    @timoschilling
    Member

    have you tried this:

        panel :test do
          # use per_page_kaminari here to get compatibility
          paginated_collection(my_collection.page(params[:page]).per_page_kaminari(15), download_links: false) do
            table_for(collection, sortable: false) do
              column :start
              column :stop
              column :duration        
            end
          end
        end
    
    Kaminari.configure do |config|
      config.page_method_name = :per_page_kaminari
    end
  5. jbmyid commented on Dec 1, 2014

    @jbmyid

    @timoschilling Yeh tried that as well but, got stack overflow infinite loop with per_page_kaminari.

  6. lanrion commented on Feb 10, 2015

    @lanrion

    @jdmyid me, too.

  7. sunloverz commented on Sep 4, 2017

    @sunloverz

    guys, using will_paginate and it doesnt work, how could I handle that issue?

  8. abrambailey commented on Dec 20, 2017

    @abrambailey

    I have the Kaminari initializer setup appropriately and using the following code. Getting stack error:

    users = role.users
    panel 'Users' do
      paginated_collection(users.page(params[:page]).per_page_kaminari(15), download_links: false) do
        table_for(collection, sortable: false) do
          column :id
         end
       end
     end
      activerecord (4.2.7) lib/active_record/relation.rb:35:in `initialize_dup'
      activerecord (4.2.7) lib/active_record/relation.rb:35:in `dup'
      activerecord (4.2.7) lib/active_record/relation.rb:35:in `initialize_copy'
      will_paginate (3.0.7) lib/will_paginate/active_record.rb:122:in `initialize_clone'
      will_paginate (3.0.7) lib/will_paginate/active_record.rb:122:in `clone'
      will_paginate (3.0.7) lib/will_paginate/active_record.rb:122:in `clone'
      will_paginate (3.0.7) lib/will_paginate/active_record.rb:50:in `first'
      will_paginate (3.0.7) lib/will_paginate/active_record.rb:52:in `first'
      will_paginate (3.0.7) lib/will_paginate/active_record.rb:52:in `first'
    
  9. filipkis commented on Apr 8, 2021

    @filipkis

    OK, with some debugging I realized what is the issue. It's the combination of .page and .per_page_kaminari that's causing the problem. If you have will_paginate gem, it's likely you renamed the Kaminari method from page to per_page_kaminari, so calling both causes an issue as first triggers the will_paginate and second then fails as it calls some methods (namely first) that get executed by will_paginate and result in stack overflow.

    So, if you're getting undefined method error when calling it with just per (as in original answer) then you should use the following version:

        panel :test do
          paginated_collection(my_collection.per_page_kaminari(params[:page]).per(15), download_links: false) do
            table_for(collection, sortable: false) do
              column :start
              column :stop
              column :duration        
            end
          end
        end

    In other words, it's not the per method that needs to be replaced, but the page method.

  10. tordans commented on Jul 30, 2021

    @tordans
    Contributor

    Just in case someone stumbles on this thread, this is what worked for me:

    Szenario: Active admin view with multiple, separately paginated lists.
    params[:somethings_page] and param_name: :somethings_page need to be unique per panel.

    Gemfile: gem 'kaminari'

    column do
      panel "PanelName" do
        paginated_collection(user.something_scope.page(params[:somethings_page]).per(10), param_name: :somethings_page, download_links: false) do
          table_for(collection) do
            column 'Titel' do |something|
              link_to(something.title, backoffice_something_path(something))
            end
          end
        end
      end
    end

    No additional code was needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions