zorab47 / active_admin-sortable_tree

Show ActiveAdmin index as a nested tree with drag'n'drop
MIT License
162 stars 127 forks source link

Enable paginating in default index view #28

Open ishumilova opened 10 years ago

ishumilova commented 10 years ago

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

nebirhos commented 10 years ago

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!

ishumilova commented 10 years ago

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

nebirhos commented 10 years ago

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

ishumilova commented 10 years ago

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

asecondwill commented 10 years ago

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?

zorab47 commented 10 years ago

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
asecondwill commented 10 years ago

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

zorab47 commented 10 years ago

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
asecondwill commented 10 years ago

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.

zorab47 commented 10 years ago

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.