Karthik T
Karthik T

Reputation: 31952

Active Admin Index Page sorting without a column

The relevant data consists of Venue, Match and Broadcast which is a join table and more for the first two.

In my Venue index page I am showing the number of Broadcasts that that venue is showing. I would like to make this sortable. Additional feature is that this should only involve Matchs in the future, and for this I have an active scope on my Broadcast model, which returns broadcasts of matches in the future.

I would like to make this column sortable. Its definition is venue.broadcasts.active.count. Since it is not a column in the DB, it doesnt seem to be sortable, and since it involves a scope that changes with time, counter column will not work by default and seems way to complicated to implement my own. Is there any good way to get this to sort? I do not care too much for performance.

Upvotes: 2

Views: 482

Answers (1)

baxang
baxang

Reputation: 3807

If you need sort an index page by a column that is not exist in a table, consider a client-side approach like DataTables, a jQuery table plugin.

I once used it for fast in-page search without hitting the database. Although it might look a little hacky, it served well for a considerably small number of records.

I believe you can use the DataTables gem as the way I googled, but I did it manually:

# config/initializers/active_admin.rb
config.register_javascript 'jquery.dataTables.js'
// app/assets/javascripts/active_admin.js
$('#index_table_on_order_orders').dataTable(); // Specify options as you need.

Upvotes: 2

Related Questions