3

The ActiveAdmin docs explain how to disable pagination and set the number of records per page in a Resource index using config.per_page, but I'm wondering if there is a built-in way to allow users to set the per_page value themselves, e.g. with a drop-down?

sixty4bit
  • 7,422
  • 7
  • 33
  • 57

3 Answers3

8

It's certainly possible using:

ActiveAdmin.register MyModel do

  before_action only: :index do
    unless params[:per_page]
      # Default pagination option if it's not already set.
      @per_page = 30
    end
  end

  # The page drop down options.
  config.per_page = [10, 20, 30, 50, 75, 100]

  # .. rest of your activeadmin code.

end
Passer by
  • 562
  • 9
  • 14
3

Unfortunately there is no such possibility. You can only either set it as default globally

config.default_per_page = 50 #in config/initializers/active_admin.rb

or set it per resource by using config.per_page as you've already mentioned.

To enable a dropdown per_page setup config/initializers/active_admin.rb: config.default_per_page = [25,50,100]
or just in a resource config.per_page = [25,50,100]

Community
  • 1
  • 1
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
2

I know this question is a couple years old, but if I came across it, someone else might and I finally found a way to accomplish this using some pretty straight forward javascript!!

My Setup:

Rails 4.2 with ActiveAdmin (1.0.0pre)

Background:

  • ActiveAdmin does interpret the per_page query string parameter (if passed in)
  • ActiveAdmin uses a hidden field #hidden_active_admin_per_page field (in the filter section of the index page) to maintain this setting across additional filtering/paging (i.e. when this setting is changed, it sends the new value as a query string parameter with the other filter criteria)

I decided to leverage this built in functionality and simply expose this "hidden" field and make it visible to the admin (as a dropdown).

Code I used

$(document).ready(function() {

  var per_page_qty = $("#hidden_active_admin_per_page").val();

  $("#hidden_active_admin_per_page").remove();

  $("form.filter_form").prepend('
    <div class="select input optional filter_form_field filter_select" id="per_page_input">
      <label for="per_page" class="label">Results Per Page</label>
      <select name="per_page" id="hidden_active_admin_per_page">
        <option selected="selected" value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="500">500</option>
        <option value="1000">1000</option>
      </select>
    </div>');

  if (typeof per_page_qty !== 'undefined') {
    $("#hidden_active_admin_per_page").val(per_page_qty);
  }
});

How it works

  1. Grab the per_page value from the hidden field which ActiveAdmin is using to manage per_page setting on the client side

    var per_page_qty = $("#hidden_active_admin_per_page").val();

  2. Remove the hidden field added by active admin (because we cannot simply unhide the hidden field and you requested a dropdown :))

    $("#hidden_active_admin_per_page").remove();

  3. Add a "visible" input back to the filter section of the form

    $("form.filter_form").prepend(...

  4. Set per_page input to value retrieved before removal of field

    $("#hidden_active_admin_per_page").val(per_page_qty);

Additional Thoughts

I hard coded the default per_page in the "pre-pended" input (i.e. select) to 25 because that was my default in my config. You could most likely inject this value from rails default setting.

Community
  • 1
  • 1
Chad Capra
  • 407
  • 4
  • 10