· 1 min read

Screencast: Suchen, Sortieren, Pagination mit AJAX

Download:

Download(20.7 MB, 13:43) Alternativer Download für iPod & Apple TV(19.9 MB, 13:43)

Resourcen:

Quellcode:

[bash] rails g jquery:install [/bash]

[ruby] # Gemfile gem ‘will_paginate’, ‘3.0.pre2’ gem ‘jquery-rails’

# products_controller.rb def index @products = Product.search(params[:search]).order(sort_column + ” ” + sort_direction).paginate(:per_page => 5, :page => params[:page]) end

# models/product.rb def self.search(search) if search where(‘name LIKE ?’, ”%#{search}%”) else scoped end end

# helpers/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, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class} end [/ruby]

[html] <%= form_tag products_path, :method => ‘get’, :id => “products_search” do %>

<%= text_field_tag :search, params[:search] %> <%= submit_tag “Search”, :name => nil %>

<%= render ‘products’ %>
<% end %>

<%= hidden\_field\_tag :direction, params\[:direction\] %> <%= hidden\_field\_tag :sort, params\[:sort\] %> <%= will\_paginate @products %> $("#products").html("<%= escape\_javascript(render("products")) %>"); \[/html\]
Back to Blog