Screencast: Sortierbare Tabellen

Ryan zeigt in diesem Screencast wie Tabellen auf- und absteigend sortiert werden können.

 

Download:

Download(16.2 MB, 10:52)
Alternativer Download für iPod & Apple TV(15.4 MB, 10:52)

 

Resourcen:

 

Quellcode:

[ruby]
class ProductsController < ApplicationController
helper_method :sort_column, :sort_direction

def index
@products = Product.order(sort_column + " " + sort_direction)
end

# …

private

def sort_column
Product.column_names.include?(params[:sort]) ? params[:sort] : "name"
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end

# application_helper.rb
def sortable(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}, {:class => css_class}
end
[/ruby]

[html]
<!– products/index.html.erb –>
<tr>
<th><%= sortable "name" %></th>
<th><%= sortable "price" %></th>
<th><%= sortable "released_at", "Released" %></th>
</tr>
[/html]

[css]
/* application.css */
.pretty th .current {
padding-right: 12px;
background-repeat: no-repeat;
background-position: right center;
}

.pretty th .asc {
background-image: url(/images/up_arrow.gif);
}

.pretty th .desc {
background-image: url(/images/down_arrow.gif);
}
[/css]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

This site uses Akismet to reduce spam. Learn how your comment data is processed.