Hi I have just followed Ryan Bates guide to loading data from the server as the client interacts with the datatable.
http://railscasts.com/episodes/340-datatables?view=asciicast
In his ProductsDatatable class he defines a sorting function:
def sort_column
columns = %w[name category released_on price]
columns[params[:iSortCol_0].to_i]
end
Basically the column name is used to query the database, so “name”, “category”, etc are all attributes to the product model. So an SQL SELECT statement is generated with Name or Category being used for sorting ASC or DESC.. and so on…
If I have data being displayed in a column that does not map explicitly to a attribute.. such as product.reviews.count, how would I then allow sorting of that column?
You would need something like this
But this means that you need to change the way you get your data, for example:
I haven’t been able to test the definition of
columns, you may have to usereviews.number_of reviewsinstead