Created
April 30, 2014 00:36
-
-
Save mathieugagne/27463cf3100a5ab44def to your computer and use it in GitHub Desktop.
Sortable Headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 files to add sorting to table headers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<th><%= sortable_header(:title) %></th> | |
<th><%= sortable_header(:description) %></th> | |
<th><%= sortable_header(:status) %></th> | |
<th><%= sortable_header(:comments_count, 'Comments') %></th> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
@projects = Project.order(sort_column => sort_direction) | |
end | |
private | |
def sort_column | |
Project.column_names.include?(params[:sort]) ? params[:sort] : "title" | |
end | |
helper_method :sort_column | |
def sort_direction | |
(['asc', 'desc'].include?(params[:direction]) ? params[:direction] : "asc").to_sym | |
end | |
helper_method :sort_direction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sortable_header (column, title = nil) | |
title ||= column.titleize | |
css_class = column == sort_column ? "current #{sort_direction}" : nil | |
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc" | |
link_to title, {sort: column, direction: direction} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment