user10387184
user10387184

Reputation:

Activeadmin - user option for hide and unhide column

Is there any solution in activeadmin for option where user can hide/unhide column?

Upvotes: 4

Views: 1720

Answers (2)

rept
rept

Reputation: 2236

I found a neat solution for this. Saving it as a session variable.

Complete solution:

This creates a button on the index page that says show or hide images depending on the state:

  action_item :toggle_image_view, only: :index do
    link_to (session['hide_images'] || false) ? t('views.admin.show_images') : t('views.admin.hide_images') , toggle_image_view_admin_person_poses_path
  end

This toggles the session key and shows the index page again:

  collection_action :toggle_image_view, method: :get do
    session['hide_images'] = !(session['hide_images'] || false)
    redirect_to collection_path
  end

Here you see the column that will get displayed or not depending on the session variable. Default is set to display them.

  index do
    selectable_column
    id_column
    column :camera
    unless (session['hide_images'] || false)
      column :image
    end
    actions
  end

Upvotes: 1

Piers C
Piers C

Reputation: 2978

Out of the box, no. You would need to define a data structure to hold the user's preferences then define your index something like:

index do
  column :title unless current_user.hide_column?(:title)
  ...
end

The simplest way to hold the preferences would be a UserColumnPreference resource which itself could be managed through ActiveAdmin. More sophisticated solutions might involve using AJAX, subclassing ActiveAdmin::IndexAsTable, etc.

If you don't need to persist the preference then a simple JavaScript to manipulate the HTML table on the page will do, eg. Hiding columns in table JavaScript This is unrelated to ActiveAdmin.

Upvotes: 1

Related Questions