Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change pagination dynamically in ActiveAdmin or solutions for printing

I'm new to Activeadmin and rails and I need some help.

I have a model that is paginated and I want to allow the user to change the pagination value or disable it completely, so it can print (to a printer) all the records (or filtered ones) for instance.

I know I can set the pagination using @per_page in :before_filter, but I can't figure out how I can change this value during execution.

To solve the problem of needing to show all the unpaginated records I defined a custom page, but in this page the filter or scope don't work so it's kind of useless.

How can I create a Print button in Active Admin?

like image 261
ciberg Avatar asked Jan 15 '23 00:01

ciberg


1 Answers

This is a workaround to do it, I know it is not the best solution but it works ! :) This is the app/admin/mymodel.rb file

ActiveAdmin.register MyModel do
  before_filter :paginate
  #other code

  controller do
    def paginate
      @per_page = params[:pagination] unless params[:pagination].blank?
    end
  end

  index do
    panel "Pagination" do
      render partial: "paginate", locals: {resource: "mymodels"}
    end
    #other code
  end

  #other code
end

And for the app/views/admin/articles/paginate.html.haml

#pagination_form
  = form_tag do 
    = label_tag :pagination, "Number of " + resource + " per page : "
    = text_field_tag :pagination
    = submit_tag "Filter"

:javascript
  $("#pagination_form form").submit(function(e){
    e.preventDefault();
    window.location = "/admin/#{resource}?pagination=" + $("#pagination").val();
  })

Hoping that my answer can people with the same problem :)

like image 80
Uelb Avatar answered Jan 30 '23 22:01

Uelb