active_admin-sortable_tree icon indicating copy to clipboard operation
active_admin-sortable_tree copied to clipboard

Enable paginating in default index view

Open ishumilova opened this issue 11 years ago • 10 comments

Hi! I've got next problem: how can I enable sorting in a default index view?

ishumilova avatar Feb 20 '14 13:02 ishumilova

Hi! Do you mean the default table view with sorting? I didn't implement it since historically you have a lot of troubles doing drag'n'drop on a table, and also you can't have nested rows. Try to change sortable.js.coffee to use the index table and see what happens!

nebirhos avatar Mar 03 '14 09:03 nebirhos

No, i mean that i need just a default index view without sorting =)

ishumilova avatar Mar 03 '14 10:03 ishumilova

If you want the sortable view without the sortable js (why?) you can do a dirty hack and set

body.active_admin .index_content .ui-sortable > * {
  pointer-events: none;
}

in your active_admin.css

nebirhos avatar Mar 07 '14 09:03 nebirhos

it's not what i want cause i want sortable tree view and plain table view)

ishumilova avatar Mar 07 '14 09:03 ishumilova

interesting question. You can easily have two index views, just declare them both in the admin.rb file, but how do we enable pagination for the table?

config.paginate = false

because the config is set to no pagination both views have no pagination. But we want the table to have pagination & the sortable view to not

index  as: :table do
     column :title
end

and

index as: :sortable do
    etc..
end

did you find a good solution for this?

asecondwill avatar May 21 '14 04:05 asecondwill

My solution is to use a before_filter to reset the config options based on the presenter.

ActiveAdmin.register Category do
  before_filter only: :index do
    if presenter_config[:as] == :sortable
      active_admin_config.paginate = false
      active_admin_config.filters  = false
    end
  end
end

zorab47 avatar May 22 '14 13:05 zorab47

@zorab47 looks great, thanks. I get an undefined variable for presenter_config - how are you getting that to work?

asecondwill avatar May 22 '14 18:05 asecondwill

Ah, yes. I left out a few key things. Here is an updated example:

ActiveAdmin.register Category do
  controller do
    before_filter only: :index do
      if sortable?
        active_admin_config.paginate = false
        active_admin_config.filters  = false
      end
    end

    def presenter_config
      active_admin_config.get_page_presenter(action_name, params[:as]) || {}
    end

    def sortable?
      presenter_config[:as] == :sortable
    end
  end
end

zorab47 avatar May 22 '14 19:05 zorab47

closer @zorab47 , but declaring the sortable tree at the top of the resource is overriding the pagination. it fetches all the items & no pagination buttons.

is there a way to make that conditional too?

btw, some really handy stuff above to know how to get at params, :as etc - thanks v much for sharing.

asecondwill avatar May 22 '14 22:05 asecondwill

There are other :as options, such as :table or :blog. Here when the presenter is the default table it will enable pagination and filters.

ActiveAdmin.register Category do
  controller do
    before_filter only: :index do
      if table_presenter?
        active_admin_config.paginate = true
        active_admin_config.filters  = true
      end
    end

    def presenter_config
      active_admin_config.get_page_presenter(action_name, params[:as]) || {}
    end

    def sortable_presenter?
      presenter_config[:as] == :sortable
    end

    def table_presenter?
      presenter_config[:as] == :table
    end
  end
end

Third time is the charm.

zorab47 avatar May 26 '14 23:05 zorab47