Skip to content
A.K.M. Ashrafuzzaman edited this page Jun 10, 2014 · 10 revisions

Customizing columns

Custom column title

By default it fetches from the I18n column translation by the following method,

Model.human_attribute_name(column_name)

You can override that with as option.

reporter(Invoice.scoped) do
  column :title, as: t('views.labels.title')
  column :invoiced_on, as: "Invoiced on"
end

Sorting

If you want to sort a column which is a column of the active record model that the query returns, then just set true to make the column sortable. If the column is from another table then you have to specify the column name.

reporter(Invoice.scoped.joins(:invoice_to)) do
  column :invoice_date, sortable: true
  column :invoice_to, sortable: 'users.name'
end

Columns to display only on web

Some of the table columns you might not want to share in the PDF or CSV, as they might be containing action links, which are not relevant in the PDF. With only_on_web option column will only be visible in the HTML view only.

reporter(Invoice.scoped) do
  column :title
  column :invoiced_on
  column :total_paid
  column :total_charged
  column :received_by_id, only_on_web: true do |invoice|
    link_to_user(invoice.user)
  end
end

Calculate total in the footer

Set true to calculate total for that column. It will render the total in the footer.

reporter(Invoice.scoped) do
  column :invoiced_to_name
  column :invoice_title
  column :amount, show_total: true
  column :paid, show_total: true
end

It will render the table as follows,

Name Invoice Amount Paid
Ashraf Invoice 1 50 50
Ashraf Invoice 2 100 10
Zahid Invoice 3 100
Total 250 60

Rowspan to group data

The rows with same values in the same column will span if set to true.

reporter(Invoice.scoped) do
  column :invoiced_to_name, rowspan: true
  column :invoice_title
  column :invoice_date, rowspan: :invoiced_to_name
end
Name Invoice Invoiced on
Ashraf Invoice 1 2-2-2014
Invoice 2
Zahid Invoice 3 2-2-2014

Note

The Invoiced on column rowspan is broken when a different name came up. Because in the last line, rowspan is set with respect to invoiced_to_name.

Pdf options

Width

To render column in pdf you can specify the width which will be delegated to PDF prawn.

reporter(Invoice.scoped) do
  column :invoiced_to_name, pdf: { width: 100 }
  column :invoice_title
end