by Dave Burt
Note: This helper is old, SortHelper2 is more recent, and Dave recommends you check that one out. Feedback, comparison, evaluations of the two is requested.
Click on the heading, and the table is sorted by that column.
The SortHelper will create links for your users to sort tables.
Here’s a quick synopsis:
View
<tr>
<th><%= link_to_sort_by 'First Name', 'name' %></th>
<th><%= link_to_sort_by 'Surname', 'family.name' %></th>
<th><%= link_to_sort_by 'Email', 'email' %></th>
</tr>
Controller:
helper :sort
def list # action
SortHelper.columns = %w[
name
family.name
email
]
SortHelper.default_order = %w[family.name name]
@people = Person.find(:all).sort do |a, b|
SortHelper.sort(a, b, @params)
end
end
And your @people are sorted!
Wouldn’t this work better if the helper just set an @param[:order_by] and then use:
@people = Person.find :all, :order => @param[:order_by]Good idea. This way sorting would work with paginators as well.
How would you modify the sort helper to include this parameter functionality?
category:Helper
by Dave Burt
Note: This helper is old, SortHelper2 is more recent, and Dave recommends you check that one out. Feedback, comparison, evaluations of the two is requested.
Click on the heading, and the table is sorted by that column.
The SortHelper will create links for your users to sort tables.
Here’s a quick synopsis:
View
<tr>
<th><%= link_to_sort_by 'First Name', 'name' %></th>
<th><%= link_to_sort_by 'Surname', 'family.name' %></th>
<th><%= link_to_sort_by 'Email', 'email' %></th>
</tr>
Controller:
helper :sort
def list # action
SortHelper.columns = %w[
name
family.name
email
]
SortHelper.default_order = %w[family.name name]
@people = Person.find(:all).sort do |a, b|
SortHelper.sort(a, b, @params)
end
end
And your @people are sorted!
Wouldn’t this work better if the helper just set an @param[:order_by] and then use:
@people = Person.find :all, :order => @param[:order_by]Good idea. This way sorting would work with paginators as well.
How would you modify the sort helper to include this parameter functionality?
category:Helper