Class: ActiveAdmin::Views::IndexAsTable

Inherits:
Component show all
Defined in:
lib/active_admin/views/index_as_table.rb

Overview

Index as a Table

By default, the index page is a table with each of the models content columns and links to show, edit and delete the object. There are many ways to customize what gets displayed.

Defining Columns

To display an attribute or a method on a resource, simply pass a symbol into the column method:

index do
  column :title
end

If the default title does not work for you, pass it as the first argument:

index do
  column "My Custom Title", :title
end

Sometimes calling methods just isn’t enough and you need to write some view specific code. For example, say we wanted a colum called Title which holds a link to the posts admin screen.

The column method accepts a block as an argument which will then be rendered within the context of the view for each of the objects in the collection.

index do
  column "Title" do |post|
    link_to post.title, admin_post_path(post)
  end
end

The block gets called once for each resource in the collection. The resource gets passed into the block as an argument.

Sorting

When a column is generated from an Active Record attribute, the table is sortable by default. If you are creating a custom column, you may need to give Active Admin a hint for how to sort the table.

If a column is defined using a block, you must pass the key to turn on sorting. The key is the attribute which gets used to sort objects using Active Record.

index do
  column "Title", :sortable => :title do |post|
    link_to post.title, admin_post_path(post)
  end
end

You can turn off sorting on any column by passing false:

index do
  column :title, :sortable => false
end

Showing and Hiding Columns

The entire index block is rendered within the context of the view, so you can easily do things that show or hide columns based on the current context.

For example, if you were using CanCan:

index do
  column :title, :sortable => false
  if can? :manage, Post
    column :some_secret_data
  end
end

Defined Under Namespace

Classes: IndexTableFor

Instance Method Summary collapse

Methods inherited from Component

#default_class_name, #initialize, #tag_name

Constructor Details

This class inherits a constructor from ActiveAdmin::Component

Instance Method Details

#build(page_config, collection) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_admin/views/index_as_table.rb', line 79

def build(page_config, collection)
  table_options = {
    :id => active_admin_config.plural_resource_name.underscore,
    :sortable => true,
    :class => "index_table",
    :i18n => active_admin_config.resource
  }

  table_for collection, table_options do |t|
    instance_exec(t, &page_config.block)
  end
end

#table_for(*args, &block) ⇒ Object



92
93
94
# File 'lib/active_admin/views/index_as_table.rb', line 92

def table_for(*args, &block)
  insert_tag IndexTableFor, *args, &block
end