activeadmin / activeadmin

The administration framework for Ruby on Rails applications.

Home Page:https://activeadmin.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pagination on table_for inside sections and panels

opened this issue · comments

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.

commented

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

Awesome, thanks huk, will give it a go!

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

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

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

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

@jdmyid me, too.

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

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'

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.

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.