Sort a table_for in rails activeadmin

后端 未结 3 894
谎友^
谎友^ 2021-02-19 15:52

In an active admin show page, I\'ve got two panels, the first being the primary record, the second being associated info (by has_many) so show looks like this:

3条回答
  •  再見小時候
    2021-02-19 16:26

    The only way I found to do it was a bit hacky. ActiveAdmin will pass in the column name and asc/desc via the params hash, then you can add that to your query.

    Also, make sure to pass "sortable: true" into the table_for call.

    panel "P&L" do
      table_for Quote.order(params[:order].gsub('_', ' ')), sortable: true do
        column("Revenue", sortable: :revenue) { |quote| number_to_currency quote.revenue }
        column("Profit", sortable: :profit)  { |quote| number_to_currency quote.profit  }
      end
    end
    

提交回复
热议问题