trestle icon indicating copy to clipboard operation
trestle copied to clipboard

Possible to set default table sort?

Open swrobel opened this issue 6 years ago • 3 comments

I'd prefer all of my tables sorted by id by default (basically as if I'd clicked the id column heading immediately after loading the page). I don't mind applying an option to each table individually to set that sort, but I don't see such an option.

swrobel avatar Nov 23 '19 02:11 swrobel

Does this help? https://github.com/TrestleAdmin/trestle/wiki/Sorting-Options

LimeBlast avatar Nov 25 '19 16:11 LimeBlast

@LimeBlast thanks for the reply. I had seen that page before, but re-reading it now, I do see how to put 2 + 2 together. I guess I would need something like this:

collection { User.order(id: :asc) }

table do
  column :id, sort: { default: true, default_order: :asc }
  # Other columns...
end

For the maintainers: I was hoping for something a little more straightforward, like table default_sort(id: :asc) and would be interested in submitting a PR to add that if it isn't possible.

swrobel avatar Nov 25 '19 22:11 swrobel

@swrobel Since default_order defaults to :asc, you can keep that line slightly simpler and do:

column :id, sort: { default: true }

However I do think there is some merit to your idea of defining the default sorting outside of the column options, as it would unify the declaration of the table column to be sorted, along with the method used to sort it.

I think something at the top level analogous to the sort_column method would be suitable. Perhaps something like this:

# Use the default sorting method as defined by the adapter
default_sort_column :id

# Specify a custom sort block
default_sort_column :id do |collection, order|
  collection.sort_by_id(order)
end

The order clause would then not need to be specified in the collection block, which also might prevent clashing order clauses in certain situations.

spohlenz avatar Nov 28 '19 10:11 spohlenz