Skip to content
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

Issue #412 paginated collection scope #428

Merged

Conversation

samvincent
Copy link
Contributor

Adding support for the following features. These were driven by the desire to display multiple paginated collections on one page in the show action. Targets Issue #412.

  • Fix scope issue with :collection method call for #paginated_entries_info
  • Add support for :param_name option
  • Add support for :download_links => false

…dCollection component

When rendering a PaginatedCollection for an associated collection within the
show context (for example), the :collection method called was referring to the resource
rather than the passed in collection. Also raising error if collection has not been
scoped with Kaminari to help new users figure it out.
…lection

Setting :param_name allows you to put multiple paginated collections
on a single page. (eg. Show action)
…llection

When rendering a paginated collection on the show screen for example,
it does not make sense to include resource download links for the collection.
@gregbell gregbell merged commit 3027431 into activeadmin:master Sep 29, 2011
@acurley
Copy link

acurley commented Sep 29, 2011

I cannot seem to get multiple paginated collections to work properly (i.e. paginate) on one show view. Could someone post an example of their show action that successfully renders multiple tables with pagination?

@samvincent
Copy link
Contributor Author

You'll want to set your param name if you're doing multiple paginated collections.

div :id => "items" do
  collection = resource.items.page(params[:item_page]).per(15)
  pagination_options = {:entry_name => Item.model_name.human, :param_name => :item_page, :download_links => false}
  paginated_collection(collection, pagination_options) do
    table_options = { :id => 'items-table', :sortable => true, :class => "index_table", :i18n => Item }
    table_for collection, table_options do
      column(:code)      { |resource| resource.item.code }
      column(:name)      { |resource| resource.item.name }
      column :created_at
    end
  end
end

@acurley
Copy link

acurley commented Sep 29, 2011

Thanks! That was very helpful! The only problem that remains is that the "Display Items" information at the top right of the table still defaults to 1-30 despite my use of .per(15). The pagination buttons on the bottom right still increment in groups of 15, but the numbers in the top right are not correct. For a class of objects that has 194 "items", the last page of paginated hits says "Displaying Items 361 - 194 of 194 in total". I'm guessing that .per(15) in this instance does not override system defaults.

@samvincent
Copy link
Contributor Author

That stuff has to do with Kaminari. You may want to take a look there and inspect your collection. I've been getting the expected results for my paginated collections so far.

@acurley
Copy link

acurley commented Sep 29, 2011

It looks like it is actually a default behavior in lib/active_admin/views/components/paginated_collection.rb. I don't see how my .per method overwrites the active_admin_application.default_per_page.

def page_entries_info(options = {})
  if collection.num_pages < 2
    ....
  else
    offset = collection.current_page * active_admin_application.default_per_page
    total  = collection.total_count
    I18n.t('active_admin.pagination.multiple', :model => entry_name.pluralize, :from => (offset - active_admin_application.default_per_page + 1), :to => offset > total ? total : offset, :total => total)
  end
end

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

Successfully merging this pull request may close these issues.

3 participants