8000 Pagination on table_for inside sections and panels · Issue #1116 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Pagination on table_for inside sections and panels #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Mar 8, 2012 · 12 comments
Closed

Pagination on table_for inside sections and panels #1116

ghost opened this issue Mar 8, 2012 · 12 comments

Comments

@ghost
Copy link
ghost commented Mar 8, 2012

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.

@huk
Copy link
huk commented Mar 21, 2012

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

@ghost
Copy link
Author
ghost commented Mar 21, 2012

Awesome, thanks huk, will give it a go!

@pcreux pcreux closed this as completed Mar 21, 2012
@jbmyid
Copy link
jbmyid commented Nov 28, 2014

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.

@timoschilling
Copy link
Member

@jbmyid
Copy link
jbmyid commented Nov 28, 2014

@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

@timoschilling
Copy link
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

@jbmyid
Copy link
jbmyid commented Dec 1, 2014

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

@lanrion
Copy link
lanrion commented Feb 10, 2015

@jdmyid me, too.

@sunloverz
Copy link

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

@abrambailey
Copy link
abrambailey commented Dec 20, 2017
8000

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'

@filipkis
Copy link
filipkis commented Apr 8, 2021

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.

@tordans
Copy link
Contributor
tordans commented Jul 30, 2021

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants
0