在rails activeadmin中对table_for进行排序 [英] Sort a table_for in rails activeadmin
问题描述
在一个活动的管理员显示页面上,我有两个面板,第一个是主记录,第二个是关联的信息(由has_many提供),所以show
看起来像这样:
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:
panel "Max Amount" do
table_for user.max_amount do
column 'Amount', :amount
column 'time', :updated_at
end
end
我可以在金额列上按从高到低(或从低到高)的顺序排序吗?
Can i sort this high-to-low (or low-to-high) on the amount column?
更新:我找到了演示的源文件( https://github.com/gregbell/demo.activeadmin.info/blob/master/app/admin/users.rb ),它似乎已排序,但是当您转到实际的演示站点时,它就无法正常工作.这个功能可能坏了吗?有解决方法吗?
Update: I found the source for the demo ( https://github.com/gregbell/demo.activeadmin.info/blob/master/app/admin/users.rb ) which seems to have sorting but when you go to the actual demo site it's not working. Is this feature perhaps broken? Is there a work-around?
推荐答案
我发现做到这一点的唯一方法是有点古怪. ActiveAdmin将通过params哈希传递列名和asc/desc,然后您可以将其添加到查询中.
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.
此外,请确保将"sortable:true"传递给table_for调用.
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
这篇关于在rails activeadmin中对table_for进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!