Skip to content
Sam Pohlenz edited this page May 21, 2018 · 1 revision

By default most columns that aren't defined with a block are sortable by the column name. If the column name doesn't map to a database column, or you have defined a custom block, you can set the sort field with the :sort option:

column :name, sort: :name_column
# or
column :name, sort: { field: :name_column }

You can set the default sort order (the order to use when you initially click on the sort icon) using the sort: { default_order: } option (default is :asc):

column :name, sort: { default_order: :desc }

If you are already applying some default sorting in the collection block (e.g. order(created_at: :desc) or similar), you can specify that column as the default sorted column:

column :created_at, sort: { default: true, default_order: :desc }

Custom column sorts can be defined using the sort_column method (within the admin block, not the table block). This is particularly useful when doing advanced sorting, e.g. applying a NULLS LAST or similar. The order block param is sanitized to be either :asc or :desc.

sort_column(:name) do |collection, order|
  collection.reorder("name_column #{order} NULLS LAST")
end