bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Question - Datagrid + page_entries_info #271

Closed rusikf closed 4 years ago

rusikf commented 4 years ago

Hi @bogdan , thanks for awesome gem!

I have a dynamic column like this select('*, CASE .. AS dynamic_column') In datagrid column I have sorting by this column(:dynamic_column, order: 'dynamic_column asc', order_desc: 'dynamic_column desc')

All works, But If I add pagination, I will have a postgres error, that column dynamic_column is not defined. It raises on page_entries_info, which simply call .assets.size and try to sort on dynamic_column , which doesn't have this dynamic column. For unknown reason it doesn't use it, it doesn't have this column only in this page_entries_info method.

Question - who to skip ordering in page_entries_info ? ( Now I use hack like this page_entries_info @reviews.assets.reorder(''))

bogdan commented 4 years ago

Postgres doesn't support orderring by aliased column and there is no magic on datagrid side that would work this around. Use these suggestiongs https://stackoverflow.com/questions/11785622/how-to-use-an-alias-in-a-postgresql-order-by-clause or duplicate the dynamic_column calculations in your order clause:

class Grid
  DYNAMIC_COLUMN = "CASE .. "
  scope { Model.select("*", "#{DYNAMIC_COLUMN} as dynamic_column") }
  column(:dynamic_column, order: '#{DYNAMIC_COLUMN} asc', order_desc: '#{DYNAMIC_COLUMN} desc')
end