Skip to content
bogdan edited this page Jan 2, 2012 · 8 revisions

Defines column that will be used to display data.

Example:

  class UserGrid 
    include Datagrid

    scope do
      User.order("users.created_at desc").includes(:group)
    end

    column(:name)
    column(:group, :order => "groups.name")
      self.group.name
    end
    column(:active, :header => "Activated") do |user|
      !user.disabled
    end

  end

Each column will be used to generate data. In order to create grid that display all users:

  grid = UserGrid.new
  grid.header # => ["Group", "Name", "Disabled"]
  grid.rows   # => [
              #      ["Steve", "Spammers", true],
              #      [ "John", "Spoilers", true],
              #      ["Berry", "Good people", false]
              #    ]
  grid.data   # => Header & Rows

Column value

Column value can be defined by passing a block to Datagrid.column method. If no block given column it is generated automatically by sending column name method to model.

  column(:name) # => asset.name

The block could have no arguments(instance_eval for each asset will be used).

  column(:completed) { self.completed? }

If you don't like instance_eval you can use asset as first argument:

  column(:completed) { |asset| asset.completed? }

You can also specify to render the block in the view context (via the :html option) :

  column(:completed, :html => true) { |asset| asset.completed? ? image_tag("green.gif") : image_tag("red.gif") }
  # or do it in partial
  column(:actions, :html => true) { |asset| render :partial => "admin/assets/actions", :object => asset }

Note that in this case html column won't appear in CSV export.

For the most complicated columns you can also pass datagrid object itself:

  filter(:category) do |value|
    where("category LIKE '%#{value}%'")
  end

  column(:exact_category) do |asset, grid|
    asset.category == grid.category
  end

Ordering

Each column supports the following options that is used to specify SQL to sort data by the given column:

  • :order - an order SQL that should be used to sort by this column. Default: report column name if there is database column with this name. Passing false will disable the order for this column.
  • :order_desc - descending order expression from this column. Default: "#{order} desc".
column(:group_name, :order => "groups.name") { self.group.name }

column(
  :priority, 
  :order => "priority is not null desc, priority", 
  :order_desc => "prioritty is not null desc, priority desc"
) # suppose that assets with null priority will be always on bottom
column(:title, :order => false) # To disable the order.

In order to specify order the following attributes are used for Datagrid instance:

  • :order - column name to sort with as Symbol. Default: nil.
  • :descending - if true descending suffix is added to specified order. Default: false.
  UserGrid.new(:order => :group, :descending => true).assets # => assets ordered by :group column descending

URLs

In order to make your column a link to some page pass :url option that should be a block that generates link:

column(:email, :url => proc {|user| "mailto:#{user.email}"})
column(:name, :url => proc {|user| Rails.application.routes.url_helpers.user_path(user) })

Localization

Column header can be specified with :header option:

column(:active, :header => "Activated")

By default it is generated from column name. Also you can use localization file if you have multilanguage application.

Example: In order to localize column :name in SimpleReport use the key datagrid.simple_report.columns.name

Clone this wiki locally